Get Configuration Information Example (C#)
This example shows how to use the SOLIDWORKS Document Manager API to get
configuration-related information for an external part document whose custom
properties were transferred from the original part 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 class into a C# console application in Microsoft
// Visual Studio.
// 3. Add the Solidworks.Interop.swdocumentmgr.dll and Microsoft.CSharp references
// to the project:
// a. Right-click the solution in Solution Explorer.
// b. Click Add Reference.
// c. Click Browse.
// d. Click install_dir\api\redist\CLR2\Solidworks.Interop.swdocumentmgr.dll.
// e. Click Add.
// f. Click Microsoft.CSharp.
// g. Click Add.
// h. Click OK.
// 4. In the code, substitute your_license_key with your SOLIDWORKS
// Document Manager license key.
// 5. Press F5 to run the program.
//
// Postconditions:
// 1. Examine the Immediate Window.
// 2. Verify that both fromparent+ prefaced and non-prefaced custom property
// values are shown.
//--------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using SolidWorks.Interop.swdocumentmgr;
class Program
{
public static void ProcessDocCustomProperties(SwDMDocument17 swDoc)
{
string[] vCustPropNameArr = null;
string sCustPropStr = null;
SwDmCustomInfoType nPropType = 0;
vCustPropNameArr = (string[])swDoc.GetCustomPropertyNames();
if ((vCustPropNameArr == null)) return;
Debug.Print(" Document Custom Properties:");
foreach (string vCustPropName in vCustPropNameArr)
{
sCustPropStr = swDoc.GetCustomProperty(vCustPropName, out nPropType);
Debug.Print(" Prefaced = " + vCustPropName + " <" + nPropType + "> = " + sCustPropStr);
sCustPropStr = swDoc.GetCustomProperty2(vCustPropName, out nPropType);
Debug.Print(" Not prefaced = " + vCustPropName + " <" + nPropType + "> = " + sCustPropStr);
Debug.Print("");
}
Debug.Print("");
}
public static void ProcessConfigCustomProperties(SwDMConfiguration14 swCfg)
{
string[] vCustPropNameArr = null;
string sCustPropStr = null;
SwDmCustomInfoType nPropType = 0;
vCustPropNameArr = (string[])swCfg.GetCustomPropertyNames();
if ((vCustPropNameArr == null)) return;
Debug.Print(" Configuration Custom Properties:");
foreach (string vCustPropName in vCustPropNameArr)
{
sCustPropStr = swCfg.GetCustomProperty(vCustPropName, out nPropType);
Debug.Print(" Prefaced = " + vCustPropName + " <" + nPropType + "> = " + sCustPropStr);
sCustPropStr = swCfg.GetCustomProperty2(vCustPropName, out nPropType);
Debug.Print(" Not prefaced = " + vCustPropName + " <" + nPropType + "> = " + sCustPropStr);
Debug.Print("");
}
Debug.Print("");
}
static void Main(string[] args)
{
const string sLicenseKey = "your_license_key";
//Specify license key
const string sDocFileName = "C:\\Users\\Public\\Documents\\SOLIDWORKS\\SOLIDWORKS 2017\\tutorial\\api\\ExternalReferencedPart.sldprt";
//Specify document
SwDMClassFactory swClassFact = default(SwDMClassFactory);
SwDMApplication swDocMgr = default(SwDMApplication);
SwDMDocument19 swDoc = default(SwDMDocument19);
SwDMConfigurationMgr swCfgMgr = default(SwDMConfigurationMgr);
string[] vCfgNameArr = null;
SwDMConfiguration14 swCfg = default(SwDMConfiguration14);
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 project if the document
// is a part or assembly document
if ((nDocType != SwDmDocumentType.swDmDocumentDrawing)) {
swClassFact = new SwDMClassFactory();
swDocMgr = swClassFact.GetApplication(sLicenseKey);
swDoc = (SwDMDocument19)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 (string) = " + swDoc.CreationDate);
Debug.Print(" CreationDate (numeric) = " + swDoc.CreationDate2);
Debug.Print(" Keywords = " + swDoc.Keywords);
Debug.Print(" LastSavedBy = " + swDoc.LastSavedBy);
Debug.Print(" LastSavedDate (string) = " + swDoc.LastSavedDate);
Debug.Print(" LastSavedDate (numeric) = " + swDoc.LastSavedDate2);
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 = (SwDMConfiguration14)swCfgMgr.GetConfigurationByName(vCfgName);
Debug.Print(" " + vCfgName);
Debug.Print(" Description = " + swCfg.Description);
Debug.Print(" ParentCfgName = " + swCfg.GetParentConfigurationName());
Debug.Print(" LastUpdateStamp = " + swCfg.GetLastUpdateStamp());
Debug.Print("");
ProcessConfigCustomProperties(swCfg);
}
}
}
}