Hello everyone,
I am developing a plugin using the SolidWorks API and encountered an issue while copying a width mate feature. The matedata.TabSelection is null, causing the mate feature creation to fail. Below is the relevant code snippet:
private void CopyWidthMate(DesignObject srcDesignObject, DesignDoc targetDoc, DocMap docMap, DesignObject targetParent)
{
IAssemblyDoc assembly = (IAssemblyDoc)targetDoc.swModel;
WidthMateFeatureDataInfo widthdata = (WidthMateFeatureDataInfo)this.mateFeatureData;
WidthMateFeatureData matedata = assembly.CreateMateData((int)this.MateType);
matedata.ConstraintType = widthdata.widthMateData.ConstraintType;
if (matedata.ConstraintType == (int)swMateWidthOptions_e.swMateWidth_Dimension)
{
matedata.DistanceFromEnd = widthdata.widthMateData.DistanceFromEnd;
matedata.FlipDimension = widthdata.widthMateData.FlipDimension;
}
Face2[] all = new Face2[widthdata.WidthSelection.Count + widthdata.TabSelection.Count];
int count = 0;
Face2[] widthsel = new Face2[widthdata.WidthSelection.Count];
for (int i = 0; i < widthdata.WidthSelection.Count; i++)
{
DesignObject srcObj1 = docMap.LeftDoc.SearchDesignObjectOfInteraface(widthdata.WidthSelection[i].Interface);
DesignObject targetObj1 = docMap.LeftToRight(srcObj1);
widthsel[i] = (Face2)targetObj1.FeatureObject.Interface;
all[count] = widthsel[i];
count++;
}
Face2[] tabSel = new Face2[widthdata.TabSelection.Count];
for (int i = 0; i < widthdata.TabSelection.Count; i++)
{
DesignObject srcObj1 = docMap.LeftDoc.SearchDesignObjectOfInteraface(widthdata.TabSelection[i].Interface);
DesignObject targetObj1 = docMap.LeftToRight(srcObj1);
tabSel[i] = (Face2)targetObj1.FeatureObject.Interface;
all[count] = tabSel[i];
count++;
}
targetDoc.swModel.ClearSelection2(true);
SelectionMgr selmrg = (SelectionMgr)targetDoc.swModel.SelectionManager;
SelectData seldata = selmrg.CreateSelectData();
seldata.Mark = 1;
int n2 = targetDoc.swModel.Extension.MultiSelect2(widthsel, true, seldata);
matedata.WidthSelection = widthsel;
SelectData seldata2 = selmrg.CreateSelectData();
seldata2.Mark = 16;
int n1 = targetDoc.swModel.Extension.MultiSelect2(tabSel, true, seldata2);
int selectedCount = targetDoc.swModel.ISelectionManager.GetSelectedObjectCount();
// Ensure tabSel array contains valid Face2 objects
for (int i = 0; i < tabSel.Length; i++)
{
tabSel[i] = (Face2)selmrg.GetSelectedObject6(i + 1, -1);
}
matedata.TabSelection = tabSel;
Feature feature = assembly.CreateMate(matedata);
if (feature == null)
{
throw new Exception("CreateMate failed when copying " + srcDesignObject);
}
}
In the above code, I attempt to select faces using the MultiSelect2 method and assign them to matedata.TabSelection. However, matedata.TabSelection remains null, causing the mate feature creation to fail.
I have checked that the tabSel array is correctly populated and ensured that it contains valid Face2 objects after the MultiSelect2 method call, but the issue persists.
Environment Information:
• SolidWorks Version: 2023
• .NET Framework Version: 4.8
• Development Environment: Visual Studio 2022
• Programming Language: C#
