Get Hidden Children Component Names Example (C#)
This example shows how to get the names of hidden children components
in an assembly document. This example also shows how to call a method
that returns multiple objects and how to cast those objects to arrays
(IComponent2::GetUnloadedComponentNames).
//-----------------------------------------------------------
// Preconditions:
// 1. In SolidWorks, click File > Open,
and browse to
// <SolidWorks_install_dir>\samples\tutorial\routing-pipes.
// 2. Select finalskid.sldasm, select
the Quick view/Selective
// open check
box, and click Open.
//
// NOTE: If
the path to the Design Library (click Tools
> Options >
// File Locations
and select Design Library in Show folders
for)
// does
not exist, then multiple dialogs might be displayed
// informing
you that SolidWorks is unable to locate
// some
components. Click No or OK to close these dialogs.
//
// 3. Expand all nodes in the FeatureManager design tree, select
// all
upper-level components except Cabinet<1>(Default),
// and
click Open Selected.
//
//
NOTE: Only
the selected components are loaded. The
// Cabinet<1>(Default)
component and its children components
// (cabinet-1, door-1, and door-2)
are not selected, not
// loaded,
and not visible in the SolidWorks graphics area.
// See
the SolidWorks Help for details about selectively
// opening
assembly documents.
//
// 4. Click OK.
// 5. Run the macro and look at the results in the Immediate
// Window
in the IDE.
//
// Postconditions: The Cabinet<1>(Default)
component and its children components
// (cabinet-1, door-1, and door-2)
are hidden.
//
// IMPORTANT:
Because this assembly document is used in
// a SolidWorks online tutorial, when you close the assembly
// document do not save any changes, if prompted.
//-----------------------------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Diagnostics;
using System.Windows.Forms;
namespace ComponentGetUnloadedChildrenComponentNames.csproj
{
public
partial class SolidWorksMacro
{
public
void Main()
{
ModelDoc2
swModel;
AssemblyDoc
swAssembly;
Component2
swComponent;
Boolean
boolStatus;
swModel
= (ModelDoc2)swApp.ActiveDoc;
swAssembly
= (AssemblyDoc)swModel;
swComponent
= (Component2)swAssembly.GetComponentByName("Cabinet-1");
boolStatus
= swComponent.HasUnloadedComponents();
if
(boolStatus == true)
{
object
oPaths = null;
object
oRefdConfigs = null;
object
oReasons = null;
object
oDocTypes = null;
object
oNames = null;
oNames
= swComponent.GetUnloadedComponentNames(out
oPaths, out oRefdConfigs, out oReasons, out oDocTypes);
string[]
Paths = null;
string[]
RefdConfigs = null;
int[]
Reasons = null;
int[]
DocTypes = null;
string[]
Names = null;
Paths
= (string[])oPaths;
RefdConfigs
= (string[])oRefdConfigs;
Reasons
= (int[])oReasons;
DocTypes
= (int[])oDocTypes;
Names
= (string[])oNames;
if
(!(Paths is System.Array) & (RefdConfigs is System.Array) & (Reasons
is System.Array) & (DocTypes is System.Array) & (Names is System.Array))
{
MessageBox.Show("Error:
Non-array parameter!");
Debug.Assert(false);
return;
}
if
((Paths.Length != RefdConfigs.Length) | (Paths.Length != Reasons.Length)
| (Paths.Length != DocTypes.Length) | (Paths.Length != Names.Length))
{
MessageBox.Show("Error:
Lengths of the arrays do not match!");
Debug.Assert(false);
return;
}
for
(int index = 0; index < Paths.Length; index++)
{
string
debugMessage = null;
debugMessage
= index + ": ";
swDocumentTypes_e
eDocType;
eDocType
= (swDocumentTypes_e)(DocTypes[index]);
switch
(eDocType)
{
case
swDocumentTypes_e.swDocNONE:
debugMessage
= debugMessage + "The document ";
break;
case
swDocumentTypes_e.swDocPART:
debugMessage
= debugMessage + "The part ";
break;
case
swDocumentTypes_e.swDocASSEMBLY:
debugMessage
= debugMessage + "The assembly ";
break;
case
swDocumentTypes_e.swDocDRAWING:
debugMessage
= debugMessage + "The drawing ";
break;
case
swDocumentTypes_e.swDocSDM:
debugMessage
= debugMessage + "The SDM ";
break;
default:
debugMessage
= debugMessage + "The document is an unknown type ";
break;
}
debugMessage
= debugMessage + Names[index] + " was not loaded because it is ";
bool
bUnloadedBecauseHidden = false;
if
(Reasons[index] == 1)
bUnloadedBecauseHidden
= true;
else
bUnloadedBecauseHidden
= false;
if
(bUnloadedBecauseHidden)
{
debugMessage
= debugMessage + "hidden.";
}
else
{
debugMessage
= debugMessage + "suppressed.";
}
Debug.Print(debugMessage);
}
}
}
///
<summary>
///
The SldWorks swApp variable is pre-assigned for you.
///
</summary>
public
SldWorks swApp;
}
}