Get Hidden Components Filenames Example (C#)
This example shows how to get the filenames of the hidden components
in an assembly. This example also shows how to call a method that returns
multiple objects and how to cast those objects to arrays (i.e., IAssemblyDoc::GetUnloadedComponentNames).
//-----------------------------------------------------------
// Preconditions:
// 1. In SolidWorks, click File >
Open, and browse to
// <SolidWorks_install_dir>\samples\tutorial\routing-pipes.
// 2. Select ball valve with flanges.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. Select Ball Valve-1 in the FeatureManager
design tree
// and
click Open Selected.
//
//
NOTE: Only
the selected components are loaded. Components
// not
selected are 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 macro and look at the results in the Immediate
// Window
in the IDE.
//
// Postconditions: Both slip
on weld flange components are hidden.
//
// NOTE:
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 Macro1.csproj
{
public
partial class SolidWorksMacro
{
public
void Main()
{
ModelDoc2
swModel;
AssemblyDoc
swAssembly;
Boolean
boolStatus;
swModel
= (ModelDoc2)swApp.ActiveDoc;
swAssembly
= (AssemblyDoc)swModel;
boolStatus
= swAssembly.HasUnloadedComponents();
if
(boolStatus == true)
{
object
oPaths = null;
object
oRefdConfigs = null;
object
oReasons = null;
object
oDocTypes = null;
object
oNames = null;
oNames
= swAssembly.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 + Paths[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;
}
}