This example shows how to add a component and a mate to an assembly.
//---------------------------------------------------------------------
// Preconditions: Open
// install_dir\Program Files\SolidWorks\samples\tutorial\toolbox\lens_mount.sldasm
//
// Postconditions:
// 1. The specified component, camtest.sldprt, and a
mate,
// top_coinc_camtest-1, are added to the assembly.
// 2. Examine the FeatureManager design tree to verify the mate.
//
// NOTE: Because the models are used elsewhere, do not
// save any changes when closing them.
//---------------------------------------------------------------------
using
SolidWorks.Interop.sldworks;
using
SolidWorks.Interop.swconst;
using
System;
using
System.Diagnostics;
using
System.Windows.Forms;
namespace
AssemblyDocAddComponentAndMate.csproj
{
partial
class
SolidWorksMacro
{
ModelDoc2 swModel;
ModelDocExtension swDocExt;
AssemblyDoc swAssy;
string
tmpPath;
ModelDoc2 tmpObj;
bool
boolstat;
Component2 swcomponent;
Feature matefeature;
string
MateName;
string
FirstSelection;
string
SecondSelection;
swMateAlign_e Alignment;
string
strCompName;
string
AssemblyTitle;
string
AssemblyName;
int
errors;
int
warnings;
int
mateError;
public
void Main()
{
swModel = (ModelDoc2)swApp.ActiveDoc;
// Get title of assembly
document
AssemblyTitle = swModel.GetTitle();
// Split the title into two
strings using the period (.) as the delimiter
string[]
strings = AssemblyTitle.Split(new
Char[] {
'.' });
// Use AssemblyName when mating
the component with the assembly
AssemblyName = (string)strings[0];
Debug.Print(AssemblyName);
boolstat = true;
string
strCompModelname = null;
strCompModelname = "camtest.sldprt";
// Because the component
resides in the same folder as the assembly, get
//
the assembly's path, strip out the assembly filename, concatenate
//
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("lens_mount.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,
0, "",
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.
//
Currently only one option,
//
swAddComponentConfigOptions_e.swAddComponentConfigOptions_CurrentSelectedConfig,
//
works for adding a part using AddComponent5
//
The other options,
//
swAddComponentConfigOptions_e.swAddComponentConfigOptions_NewConfigWithAllReferenceModels
//
and
swAddComponentConfigOptions_e.swAddComponentConfigOptions_NewConfigWithAsmStructure,
//
work only for adding assemblies using AddComponent5
swcomponent =
(Component2)swAssy.AddComponent5(strCompModelname, (int)swAddComponentConfigOptions_e.swAddComponentConfigOptions_CurrentSelectedConfig,
"",
false,
"", -1,
-1, -1);
// Get the name of the
component for the mate
strCompName =
swcomponent.Name2;
// Create the name of the mate
and the names of the planes to use for the mate
MateName =
"top_coinc_" +
strCompName;
FirstSelection = "Top@"
+ strCompName + "@"
+ AssemblyName;
SecondSelection = "Front@"
+ AssemblyName;
swDocExt = (ModelDocExtension)swModel.Extension;
swModel.ClearSelection2(true);
// Select the planes for the
mate
boolstat = swDocExt.SelectByID2(FirstSelection,
"PLANE",
0, 0, 0, true,
1, null, (int)swSelectOption_e.swSelectOptionDefault);
boolstat = swDocExt.SelectByID2(SecondSelection,
"PLANE", 0, 0,
0, true, 1,
null, (int)swSelectOption_e.swSelectOptionDefault);
// Add the mate
matefeature = (Feature)swAssy.AddMate3((int)swMateType_e.swMateCOINCIDENT,
(int)swMateAlign_e.swMateAlignALIGNED,
false, 0,
0, 0, 0, 0, 0, 0,
0, false,
out
mateError);
matefeature.Name = MateName;
swModel.ViewZoomtofit2();
}
public
SldWorks swApp;
}
}