Get the Current Name of the Configuration of a Suppressed Component (C#)
This example shows how to obtain the current name of the configuration of a
suppressed component.
//----------------------------------------------------------------------------
// 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 Program.cs of 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:
// install_dir\api\redist\SolidWorks.Interop.swdocumentmgr.dll
// 4. Also add the following references to the project:
// System
// System.Core
// System.Data
//
System.Data.DataSetExtensions
// System.Xml
//
System.Xml.Linq
// 5. Substitute your_license_code with your SolidWorks
// Document Manager license key.
// 6. Substitute assembly_with_suppressed_comps
with the path to an assembly
// that
contains one or more
suppressed components.
// 7. Open an Output window.
//
// Postconditions: Inspect the Output window for configuration
information.
//
// NOTE: This console application was developed using
// Microsoft Visual Studio 2008. If you use another version of
// Microsoft Visual Studio, you may need to add more references to get
// this program to compile and open an Immediate window to view output.
//----------------------------------------------------------------------------
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
SolidWorks.Interop.swdocumentmgr;
using
System.Diagnostics;
using
System.Collections;
using
System.IO;
namespace
ConsoleApplication1
{
class
Program
{
static
void Main(string[]
args)
{
//Specify your license key
const
string
sLicenseKey = "your_license_code";
const
string
sDocFileName = "assembly_with_suppressed_comps";
SwDMClassFactory swClassFact =
default(SwDMClassFactory);
SwDMApplication3
swDocMgr = default(SwDMApplication3);
SwDMDocument15
swDoc = default(SwDMDocument15);
SwDMDocument15
swDoc2 = default(SwDMDocument15);
SwDmDocumentOpenError
nRetVal = 0;
SwDMExternalReferenceOption
dmExtRefOption;
SwDMSearchOption
dmSearchOpt;
int
numExtRefs;
swClassFact = new
SwDMClassFactory();
swDocMgr = (SwDMApplication3)swClassFact.GetApplication(sLicenseKey);
swDoc = (SwDMDocument15)swDocMgr.GetDocument(sDocFileName,
GetDocType(sDocFileName), false,
out
nRetVal);
Debug.Assert(SwDmDocumentOpenError.swDmDocumentOpenErrorNone
== nRetVal);
Debug.Print("File
= " + swDoc.FullName);
dmExtRefOption = swDocMgr.GetExternalReferenceOptionObject();
dmSearchOpt = swDocMgr.GetSearchOptionObject();
dmExtRefOption.SearchOption = dmSearchOpt;
dmExtRefOption.Configuration =
"Default";
dmExtRefOption.NeedSuppress =
true;
numExtRefs = swDoc.GetExternalFeatureReferences(ref
dmExtRefOption);
Array
arrExtRefs = (Array)(dmExtRefOption.ExternalReferences);
SwDMConfiguration12
config;
SwDMConfigurationMgr
configMgr = swDoc.ConfigurationManager;
config=(SwDMConfiguration12)configMgr.GetConfigurationByName(configMgr.GetActiveConfigurationName());
object
comps=config.GetComponents();
Array
arrComps = (Array)comps;
foreach
(SwDMComponent8
swComp in
arrComps)
{
Debug.Print("Component
Name: " + swComp.Name2);
Debug.Print("Component
Configuration Name: " + swComp.ConfigurationName);
Debug.Print("Component
Configuration ID: " + swComp.ConfigurationID);
string
ComponentPathName = swComp.PathName;
//Check validity of the
Component's path name. It might be out of date if the path changed
after
//the
component was suppressed.
if
(!File.Exists(ComponentPathName))
//If that file
cannot be found, look for an external reference with the same name
ComponentPathName =
FindExtRefPath(swComp.PathName, arrExtRefs);
swDoc2 = (SwDMDocument15)swDocMgr.GetDocument(ComponentPathName,
GetDocType(ComponentPathName), false,
out
nRetVal);
SwDMConfiguration12
config2;
SwDMConfigurationMgr
configMgr2 = swDoc2.ConfigurationManager;
string[]
confignames = (string[])configMgr2.GetConfigurationNames();
ArrayList
arrConfigNames = new
ArrayList(confignames);
// If the configuration
name for the component doesn't match an
//
existing config name in the reference part, go find the updated config
name
//
from the part.
if
(!arrConfigNames.Contains(swComp.ConfigurationName))
{
foreach
(string
configName in
arrConfigNames)
{
config2 = (SwDMConfiguration12)configMgr2.GetConfigurationByName(configName);
if
(config2.GetID() == swComp.ConfigurationID)
Debug.Print("Reference
Part Configuration Name: " + config2.Name2);
}
}
swDoc2.CloseDoc();
}
}
static
string
FindExtRefPath(string
name, Array
arrComps)
{
string
ExtrefPathName = name;
string[]
nameParts = name.Split('\\');
string
justName = nameParts.GetValue(nameParts.GetUpperBound(0)).ToString();
int
i = arrComps.GetLowerBound(0);
Boolean
found = false;
while
((i<=arrComps.GetUpperBound(0)) && !found)
{
string
extref = (arrComps.GetValue(i)).ToString();
string[]
extrefParts = extref.Split('\\');
string
justextrefName = extrefParts.GetValue(extrefParts.GetUpperBound(0)).ToString();
if
(justextrefName == justName)
{
found = true;
ExtrefPathName = extref;
}
i++;
}
return
ExtrefPathName;
}
static
SwDmDocumentType GetDocType(string
name)
{
SwDmDocumentType
nDocType = SwDmDocumentType.swDmDocumentUnknown;
if
(name.EndsWith("SLDPRT"))
{
nDocType =
SwDmDocumentType.swDmDocumentPart;
}
else
if (name.EndsWith("SLDASM"))
{
nDocType =
SwDmDocumentType.swDmDocumentAssembly;
}
return
nDocType;
}
}
}