Search code examples
c#solidworks

Problems with EquationMgr & SelectionManger Solidworks Api C#


I'm trying to understand principles of solidworks API, but have several problems. Here is my code:

for (var i = 0; i < selMgr.GetSelectedObjectCount(); i++)
        {
            var Face = selMgr.GetSelectedObject(i+1);
            surfaces.Add(Face.GetSurface());
            measure = swModel.Extension.CreateMeasure();

            if (surfaces[i].IsCylinder())
            {

                // Problem # 1

                Console.WriteLine("Cylinder " + i);
                measure.Calculate(surfaces[i]);
                var diameter = measure.Diameter * 1000;
                var length = 1000 * measure.Perimeter  / (measure.Diameter * Math.PI);

                var temp = swApp.OpenDoc6(@"E:\OAK\Locator9.SLDPRT", 1, 1, "", 0, 0);
                var part = component.AddComponent5(@"E:\OAK\Locator9.SLDPRT", 0, "", true, "", 0, 0, 0.3);
                swApp.CloseDoc(@"E:\OAK\Locator9.SLDPRT");

                ModelDoc2 locator = part.GetModelDoc();

                var eqMgr = locator.GetEquationMgr();

                Console.WriteLine("Evaluated diameter " + diameter);
                Console.WriteLine("Evaluated length " + length);

                Console.WriteLine(eqMgr.Equation[1] + "   " + eqMgr.Equation[2]);


                //Problem #2


                eqMgr.set_Equation(1, $@"""D""={diameter}");
                eqMgr.set_Equation(2, $@"""L""={length}");
                eqMgr.EvaluateAll();
                locator.EditRebuild3();
                locator.ForceRebuild3(false);
            }
            else
            {
                // TODO: Handle other type of surface 
            }
        }

1) I want to measure perimeter & diameter of the selected surface. But if a return value of GetSelectedObjectCount() method is greater than 1, measure.Diameter & measure.Perimeter both returns -1. And I kinda understand why, 'cause such operation isn't possible via UI as well, but can I do smth to solve the problem?
2) The code above has no influence on the equation of the inserted component, even if it writes it on the console. Help please!


Solution

  • 1 For primitive surfaces you can use *Params property of the ISurface object to get the information you need. For cylinder it would be CylinderParams. I can't find the link right now but I remember reading that measure shouldn't be used for any precise calculations as it is not guaranteed to be accurate at all times. If you don't care about precision and still want to use measure you can manually manipulate set of selected objects.

    2 I haven't used IEquationMgr but in general I tried to stay away from VB styled parameterized properties like Equation , I'd suggest trying to Delete and then Add equation.