3 Replies Latest reply on Aug 7, 2018 5:59 PM by Zhen Yan

    IModeler Project Curve onto Surface

    Zhen Yan

      I'm writing an add-in. It's in C#, but solutions in other languages are fine too as I'm not new to programming.

       

      I'm trying to project curves onto surfaces without planar guarantees. I finally got the calls to work despite the anemic documentation that is IModeler and IBody2::Display3(), however, my lines stay straight instead of being warped to stick to the surface. Is this expected behavior or am I doing something wrong?

       

      projected line.png

      This is the code I'm playing around with

       

      static void Main(string[] args)
      {
          SldWorks sld = new SldWorks();
          ModelDoc2 model = sld.ActiveDoc;
          Modeler modeler = sld.GetModeler();
          SelectionMgr sm = model.SelectionManager;
      
          Console.Write("Make a selection");
          Console.ReadLine();
          if (sm.GetSelectedObject6(1, -1) is Edge edge) // gets the edge whose curve you want to project
          {
              Console.WriteLine("Selection made");
              Console.WriteLine();
      
              Curve curve = edge.GetCurve();
              Curve copy = curve.Copy();
      
              // Start() and End() are my extensions. Returns an IVertex
              var start = edge.Start().GetPoint();
              var end = edge.End().GetPoint();
      
              Curve trimmed = copy.CreateTrimmedCurve2(start[0], start[1], start[2], end[0], end[1], end[2]);
      
              Console.WriteLine("Select a face");
              Console.ReadLine();
              if (sm.GetSelectedObject6(1, -1) is Face2 face) // gets the face to project onto
              {
                  Console.WriteLine("Face selected");
                  Surface surface = face.GetSurface(); // the surface to project onto
      
                  MathUtility utility = sld.GetMathUtility();
                  double[] norm = { 0, -1, 0 };
                  MathVector vnorm = utility.CreateVector(norm);
      
                  Curve converted = trimmed.MakeBsplineCurve2();
                  Curve projected = modeler.ProjectCurveOnSurface(converted, surface, vnorm);
                  
                  Body2 temp = projected.CreateWireBody();
                  temp.Display3(model, Information.RGB(255, 255, 0), (int)swTempBodySelectOptions_e.swTempBodySelectOptionNone);
                  // ^^ displays it, we're done ^^
              }
              else
                  Console.WriteLine("No face selected");
          }
          else
              Console.WriteLine("Bad selection");
      
          Console.WriteLine();
          Console.WriteLine("Exit with any key");
          Console.ReadKey();
      
          Marshal.FinalReleaseComObject(sld);
          sld = null;
          GC.Collect();
      }