Get Configuration Information Example (C#)
This example shows how to get configuration-related information from a
part or assembly document.
//----------------------------------------------------------------------------
// Preconditions:
// 1. Read the SolidWorks Document Manager API Getting Started topic
// and ensure that the required DLLs have been registered.
// 2. Copy and paste this code into a C# console application
// in Microsoft Visual Studio.
// 3. Add the Solidworks.Interop.swdocumentmgr.dll reference
// to the project:
// a. Right-click the solution in Solution Explorer.
// b. Select Add Reference.
// c. Click the Browse tab.
// d. Select:
// <SolidWorks_install_dir>\api\redist\Solidworks.Interop.swdocumentmgr.dll
// 4. Substitute <your_license_code> with your SolidWorks
// Document Manager license key.
// 5. Ensure that the specified part exists.
// 6. Open an Immediate window.
//
// Postconditions: Inspect the Immediate window for configuration
information.
//----------------------------------------------------------------------------
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Diagnostics;
using
SolidWorks.Interop.swdocumentmgr;
class
Program
{
public
static
void
ProcessDocCustomProperties(SwDMDocument15
swDoc)
{
string[]
vCustPropNameArr = null;
string
sCustPropStr = null;
SwDmCustomInfoType
nPropType = 0;
vCustPropNameArr = (string[])swDoc.GetCustomPropertyNames();
if
((vCustPropNameArr == null))
return;
Debug.Print("
Custom Properties:");
foreach
(string
vCustPropName in
vCustPropNameArr)
{
sCustPropStr = swDoc.GetCustomProperty(vCustPropName,
out nPropType);
Debug.Print("
" + vCustPropName +
" <" +
nPropType + "> = "
+ sCustPropStr);
}
Debug.Print("");
}
public
static
void
ProcessConfigCustomProperties(SwDMConfiguration13
swCfg)
{
string[]
vCustPropNameArr = null;
string
sCustPropStr = null;
SwDmCustomInfoType
nPropType = 0;
vCustPropNameArr = (string[])swCfg.GetCustomPropertyNames();
if
((vCustPropNameArr == null))
return;
Debug.Print("
Custom Properties:");
foreach
(string
vCustPropName in
vCustPropNameArr)
{
sCustPropStr = swCfg.GetCustomProperty(vCustPropName,
out nPropType);
Debug.Print("
" + vCustPropName +
" <" +
nPropType + "> = "
+ sCustPropStr);
}
Debug.Print("");
}
static
void
Main(string[]
args)
{
const
string
sLicenseKey = "<your_license_code>";
//Specify license key
const
string
sDocFileName = "<SolidWorks_install_dir>\\samples\\tutorial\\weldments\\weldment_box2.sldprt";
//Specify document
SwDMClassFactory
swClassFact = default(SwDMClassFactory);
SwDMApplication3
swDocMgr = default(SwDMApplication3);
SwDMDocument15
swDoc = default(SwDMDocument15);
SwDMConfigurationMgr
swCfgMgr = default(SwDMConfigurationMgr);
string[]
vCfgNameArr = null;
SwDMConfiguration13
swCfg = default(SwDMConfiguration13);
SwDmDocumentType
nDocType = 0;
SwDmDocumentOpenError
nRetVal = 0;
// Determine type of SolidWorks
file based on file extension
if
(sDocFileName.EndsWith("sldprt"))
{
nDocType =
SwDmDocumentType.swDmDocumentPart;
}
else
if (sDocFileName.EndsWith("sldasm"))
{
nDocType =
SwDmDocumentType.swDmDocumentAssembly;
}
else
if (sDocFileName.EndsWith("slddrw"))
{
nDocType =
SwDmDocumentType.swDmDocumentDrawing;
}
else
{
// Not a SolidWorks file
nDocType =
SwDmDocumentType.swDmDocumentUnknown;
// So cannot open
return;
}
// Because drawing documents do
not have configurations,
//
only continue running the macro if the document
//
is a part or assembly document
if
((nDocType != SwDmDocumentType.swDmDocumentDrawing))
{
swClassFact = new
SwDMClassFactory();
swDocMgr =
(SwDMApplication3)swClassFact.GetApplication(sLicenseKey);
swDoc = (SwDMDocument15)swDocMgr.GetDocument(sDocFileName,
nDocType, false,
out
nRetVal);
Debug.Assert(SwDmDocumentOpenError.swDmDocumentOpenErrorNone
== nRetVal);
swCfgMgr = swDoc.ConfigurationManager;
Debug.Print("File
= " + swDoc.FullName);
Debug.Print("
ActiveCfgName = " + swCfgMgr.GetActiveConfigurationName());
Debug.Print("");
Debug.Print("
Version = " + swDoc.GetVersion());
Debug.Print("
Author = " + swDoc.Author);
Debug.Print("
Comments = " + swDoc.Comments);
Debug.Print("
CreationDate = " + swDoc.CreationDate);
Debug.Print("
Keywords = " + swDoc.Keywords);
Debug.Print("
LastSavedBy = " + swDoc.LastSavedBy);
Debug.Print("
LastSavedDate = " + swDoc.LastSavedDate);
Debug.Print("
Subject = " + swDoc.Subject);
Debug.Print("
Title = " + swDoc.Title);
Debug.Print("
LastUpdateStamp = " + swDoc.GetLastUpdateStamp());
Debug.Print("
IsDetachedDrawing = " + swDoc.IsDetachedDrawing());
Debug.Print("");
ProcessDocCustomProperties(swDoc);
vCfgNameArr = (string[])swCfgMgr.GetConfigurationNames();
foreach
(string
vCfgName in
vCfgNameArr)
{
swCfg = (SwDMConfiguration13)swCfgMgr.GetConfigurationByName(vCfgName);
Debug.Print("
" + vCfgName);
Debug.Print(" Description =
" + swCfg.Description);
Debug.Print(" ParentCfgName =
" + swCfg.GetParentConfigurationName());
Debug.Print(" LastUpdateStamp =
" + swCfg.GetLastUpdateStamp());
Debug.Print(" Type
as defined in swDmConfigurationType = "
+ swCfg.type);
Debug.Print("");
ProcessConfigCustomProperties(swCfg);
}
}
}
}