Add Smart Components Example (C#)
This example shows how to add Smart Components to an assembly.
//---------------------------------------------------------------------------
// Preconditions:
// Open:
// <SOLIDWORKS_install_dir>\samples\tutorial\smartcomponents\stepped_shaft.sldasm
//
// Postconditions:
// 1.
<SOLIDWORKS_install_dir>\samples\tutorial\smartcomponents\Bearing.sldprt
// is
opened.
// 2.
Bearing.sldprt is added
// as
three separate Smart Components at the
// specified
coordinates to stepped_shaft.sldasm.
//
// NOTE: Because
this assembly is used in a SOLIDWORKS
// online tutorial, do not save any changes
// when you close the assembly document.
//---------------------------------------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Windows.Forms;
using System.Diagnostics;
namespace AddSmartComponentsCSharp.csproj
{
public
partial class SolidWorksMacro
{
public
void Main()
{
ModelDoc2
swModel;
AssemblyDoc
swAssy;
string
tmpPath;
ModelDoc2
tmpObj;
Component2
swcomponent;
string
AssemblyTitle;
int
errors = 0;
int
warnings = 0;
bool
boolstat;
swModel
= (ModelDoc2)swApp.ActiveDoc;
//
Get title of assembly document
AssemblyTitle
= swModel.GetTitle();
boolstat
= true;
string
strCompModelname = null;
strCompModelname
= "Bearing.sldprt";
//
Because the component resides in the same folder as the assembly, get
//
the assembly's path, strip out the assembly filename, concatenate the
//
the rest of the path to the component filename, and use
//
this string to open the component
tmpPath
= swModel.GetPathName();
int
idx;
idx
= tmpPath.LastIndexOf("stepped_shaft.sldasm");
string
compPath;
tmpPath
= tmpPath.Substring(0, (idx));
compPath
= string.Concat(tmpPath, strCompModelname);
//
Open the component
tmpObj
= (ModelDoc2)swApp.OpenDoc6(compPath,
(int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent,
"", ref errors, ref warnings);
//
Check to see if the file is read-only or cannot be found; display error
//
messages if either
if
(warnings == (int)swFileLoadWarning_e.swFileLoadWarning_ReadOnly) {
MessageBox.Show("This
file is read-only.");
boolstat
= false;
}
if
(tmpObj == null) {
MessageBox.Show("Cannot
locate the file.");
boolstat
= false;
}
//Re-activate
the assembly so that you can add the component to it
swModel
= (ModelDoc2)swApp.ActivateDoc2(AssemblyTitle,
true, ref errors);
swAssy
= (AssemblyDoc)swModel;
//
Add the component to the assembly document and
//
print the component name to the Immediate Window
swcomponent
= swAssy.AddSmartComponent(strCompModelname,
0, 0.13, -0.1);
Debug.Print(swcomponent.Name2);
swcomponent
= swAssy.AddSmartComponent(strCompModelname,
0, 0.13, -0.42);
Debug.Print(swcomponent.Name2);
swcomponent
= swAssy.AddSmartComponent(strCompModelname,
0, 0.13, 0.49);
Debug.Print(swcomponent.Name2);
swModel.ClearSelection2(true);
}
///
<summary>
///
The SldWorks swApp variable is pre-assigned for you.
///
</summary>
public
SldWorks swApp;
}
}