Insert Mate Reference Example (C#)
This example shows how to insert a mate reference.
//---------------------------------------------------------------------------
// Preconditions: Open:
//<SolidWorks_install_dir>\samples\HandsOn\mate references\crank-arm.sldprt
//
// Postconditions:
// A
mate reference, MateReference1, is inserted in the MateReferences folder
//
in the FeatureManager design tree.
// NOTE:
Because this part is used in a SolidWorks online tutorial,
// do not save
any changes when you close it.
//---------------------------------------------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
namespace InsertMateReference2_CSharp.csproj
{
partial
class SolidWorksMacro
{
public
void Main()
{
ModelDoc2
swModel = default(ModelDoc2);
ModelDocExtension
swModelDocExt = default(ModelDocExtension);
SelectionMgr
swSelMgr = default(SelectionMgr);
FeatureManager
swFeatMgr = default(FeatureManager);
Feature
swFeature = default(Feature);
Entity
mateRef1 = null;
Entity
mateRef2 = null;
Entity
mateRef3 = null;
bool
boolstatus = false;
string
strMateRefName = null;
swModel
= (ModelDoc2)swApp.ActiveDoc;
swModelDocExt
= swModel.Extension;
swSelMgr
= (SelectionMgr)swModel.SelectionManager;
swFeatMgr
= swModel.FeatureManager;
swModel.ClearSelection2(true);
//
Select three faces
boolstatus
= swModel.Extension.SelectByID2("", "FACE", 0.007841429441271,
0.004702510345794, 0.007840974544081, false, 0, null, 0);
mateRef1
= (Entity)swSelMgr.GetSelectedObject6(1, -1);
boolstatus
= swModel.Extension.SelectByID2("", "FACE", 0.06256195446167,
0.003271675226301, 0.004687091733047, false, 0, null, 0);
mateRef2
= (Entity)swSelMgr.GetSelectedObject6(1, -1);
boolstatus
= swModel.Extension.SelectByID2("", "FACE", 0.04319534991731,
0.006350000000054, 0.009894144482189, false, 0, null, 0);
mateRef3
= (Entity)swSelMgr.GetSelectedObject6(1, -1);
swModel.ClearSelection2(true);
strMateRefName
= "MateReference1";
//
Insert mate reference
swFeature
= swFeatMgr.InsertMateReference2(strMateRefName,
(Entity)mateRef1, (int)swMateReferenceType_e.swMateReferenceType_Concentric,
(int)swMateReferenceAlignment_e.swMateReferenceAlignment_Aligned, false,
mateRef2, (int)swMateReferenceType_e.swMateReferenceType_Concentric, (int)swMateReferenceAlignment_e.swMateReferenceAlignment_AntiAligned,
false, mateRef3,
(int)swMateReferenceType_e.swMateReferenceType_Concentric,
(int)swMateReferenceAlignment_e.swMateReferenceAlignment_Closest);
if
(swFeature == null)
{
System.Windows.Forms.MessageBox.Show("Failed
to insert mate reference.");
}
swModel.ClearSelection2(true);
}
public
SldWorks swApp;
}
}