Hello Everyone,
I've been experimenting with SolidWorks API using C# for a few weeks now. So far I've been able to solve every other problem with the help of this forum, Google and SolidWorks API reference. This simple mating problem I am facing now has been very persistent despite great efforts to find the cause.
With the below codes, I am not able to create distance mates or angle mates. The strange thing is that I can create coincident mates without a problem with almost identical code. I am using SelectByID2 with the append option and marking the selections with 1. Then I'm creating the mate data, setting the appropriate properties and calling CreateMate with this mate data. For distance mates and angle mates CreateMate returns null and nothing happens. For coincident mate everything works perfectly.
Any help would be greatly appreciated. I have put the code below.
Thanks.
// Code for Distance Mate (CreateMate returns null, not working)
swApp = (SldWorks)Marshal.GetActiveObject("SldWorks.Application");
Part = swApp.OpenDoc6(@"C:\Users\pkn\Desktop\Assem.SLDASM", 2, 0, "", ref longstatus, ref longwarnings);
swAsm = (AssemblyDoc)Part;
Part.ClearSelection2(true);
Part.Extension.SelectByID2("Mate_Plane_HingeAngle@TestCube-3@Assem", "PLANE", 0, 0, 0, true, 1, null, 0);
Part.Extension.SelectByID2("Mate_Plane_HingeAngle@TestCube-4@Assem", "PLANE", 0, 0, 0, true, 1, null, 0);
DistanceMateFeatureData distanceMateData;
distanceMateData = swAsm.CreateMateData((int)swMateType_e.swMateDISTANCE);
distanceMateData.Distance = 1;
swAsm.CreateMate(distanceMateData);
Part.EditRebuild3();
// Code for Angle Mate (CreateMate returns null, not working)
swApp = (SldWorks)Marshal.GetActiveObject("SldWorks.Application");
Part = swApp.OpenDoc6(@"C:\Users\pkn\Desktop\Assem.SLDASM", 2, 0, "", ref longstatus, ref longwarnings);
swAsm = (AssemblyDoc)Part;
Part.ClearSelection2(true);
Part.Extension.SelectByID2("Mate_Plane_HingeAngle@TestCube-3@Assem", "PLANE", 0, 0, 0, true, 1, null, 0);
Part.Extension.SelectByID2("Mate_Plane_HingeAngle@TestCube-4@Assem", "PLANE", 0, 0, 0, true, 1, null, 0);
AngleMateFeatureData angleMateData;
MateFeatureData matDat;
matDat = swAsm.CreateMateData((int)swMateType_e.swMateANGLE);
angleMateData = (AngleMateFeatureData)matDat;
angleMateData.Angle = 0.5;
angleMateData.MateAlignment = (int)swMateAlign_e.swMateAlignALIGNED;
angleMateData.FlipDimension = false;
angleMateData.MaximumAngle = 0.523599;
angleMateData.MinimumAngle = 0.506145;
swAsm.CreateMate(angleMateData);
Part.EditRebuild3();
// Code for Coincident Mate, works properly
swApp = (SldWorks)Marshal.GetActiveObject("SldWorks.Application");
Part = swApp.OpenDoc6(@"C:\Users\pkn\Desktop\Assem.SLDASM", 2, 0, "", ref longstatus, ref longwarnings);
swAsm = (AssemblyDoc)Part;
Part.ClearSelection2(true);
Part.Extension.SelectByID2("Mate_Plane_HingeAngle@TestCube-3@Assem", "PLANE", 0, 0, 0, true, 1, null, 0);
Part.Extension.SelectByID2("Mate_Plane_HingeAngle@TestCube-4@Assem", "PLANE", 0, 0, 0, true, 1, null, 0);
CoincidentMateFeatureData mateData;
mateData = swAsm.CreateMateData((int)swMateType_e.swMateCOINCIDENT);
swAsm.CreateMate(mateData);
Part.EditRebuild3();