4 Replies Latest reply on Feb 22, 2018 10:32 PM by Jacob Corder

    CreateTrimmedCurve2 Not Working Correctly?

    Brian Smith

      I feel like I may be doing something terrible wrong, but I can't quite figure out what is wrong with this function. CreateTrimmedCurve2 does not seem to act predictably. Below are 4 examples that all I am doing is altering the start and direct of the beginning curve and the start and end points of the createtrimmedcurve2 function.

       

      Option 1: The below code runs fine, and displays a temporary line.

       

      Sub main()

          Dim swApp As SldWorks.SldWorks

          Dim swModeler As SldWorks.Modeler

          Dim swCurve As SldWorks.Curve

          Dim trimmed_curve As SldWorks.Curve

          Dim nPt(2) As Double

          Dim nPte(2) As Double

          Dim vPt As Variant

          Dim vDir As Variant

          Dim retval As Long

          Dim swPart As SldWorks.PartDoc

         

          Set swApp = Application.SldWorks

          Set swModeler = swApp.GetModeler

          Set swPart = swApp.ActiveDoc

          nPt(0) = 0

          nPt(1) = 0

          nPt(2) = 0

          vPt = nPt

          nPte(0) = 0

          nPte(1) = 0

          nPte(2) = 1

          vDir = nPte

          Set swCurve = swModeler.CreateLine(vPt, vDir)

          Set trimmed_curve = swCurve.CreateTrimmedCurve2(0, 0, 0, 0, 0, 1)

          Set swBody = trimmed_curve.CreateWireBody()

          retval = swBody.Display3(swPart, 255, swTempBodySelectable)

          Stop

      End Sub

       

       

      Option 2: The below code runs fine, and displays a temporary line.

       

      Sub main()

          Dim swApp As SldWorks.SldWorks

          Dim swModeler As SldWorks.Modeler

          Dim swCurve As SldWorks.Curve

          Dim trimmed_curve As SldWorks.Curve

          Dim nPt(2) As Double

          Dim nPte(2) As Double

          Dim vPt As Variant

          Dim vDir As Variant

          Dim retval As Long

          Dim swPart As SldWorks.PartDoc

         

          Set swApp = Application.SldWorks

          Set swModeler = swApp.GetModeler

          Set swPart = swApp.ActiveDoc

          nPt(0) = -2

          nPt(1) = -2

          nPt(2) = -2

          vPt = nPt

          nPte(0) = 2

          nPte(1) = 2

          nPte(2) = 2

          vDir = nPte

          Set swCurve = swModeler.CreateLine(vPt, vDir)

          Set trimmed_curve = swCurve.CreateTrimmedCurve2(-1, -1, -1, 1, 1, 1)

          Set swBody = trimmed_curve.CreateWireBody()

          retval = swBody.Display3(swPart, 255, swTempBodySelectable)

          Stop

      End Sub

       

       

      Option 3: The below errors out at the bold line because I believe it is not creating the trimmed_curve object.

       

      Sub main()

          Dim swApp As SldWorks.SldWorks

          Dim swModeler As SldWorks.Modeler

          Dim swCurve As SldWorks.Curve

          Dim trimmed_curve As SldWorks.Curve

          Dim nPt(2) As Double

          Dim nPte(2) As Double

          Dim vPt As Variant

          Dim vDir As Variant

          Dim retval As Long

          Dim swPart As SldWorks.PartDoc

         

          Set swApp = Application.SldWorks

          Set swModeler = swApp.GetModeler

          Set swPart = swApp.ActiveDoc

          nPt(0) = -2

          nPt(1) = 0

          nPt(2) = 0

          vPt = nPt

          nPte(0) = 2

          nPte(1) = 0

          nPte(2) = 1

          vDir = nPte

          Set swCurve = swModeler.CreateLine(vPt, vDir)

          Set trimmed_curve = swCurve.CreateTrimmedCurve2(-1, 0, 0, 1, 0, 1)

         Set swBody = trimmed_curve.CreateWireBody()

          retval = swBody.Display3(swPart, 255, swTempBodySelectable)

          Stop

      End Sub

       

      Option 4: Similarly, the below errors out at the bold line because I believe it is not creating the trimmed_curve object.

       

      Sub main()

          Dim swApp As SldWorks.SldWorks

          Dim swModeler As SldWorks.Modeler

          Dim swCurve As SldWorks.Curve

          Dim trimmed_curve As SldWorks.Curve

          Dim nPt(2) As Double

          Dim nPte(2) As Double

          Dim vPt As Variant

          Dim vDir As Variant

          Dim retval As Long

          Dim swPart As SldWorks.PartDoc

         

          Set swApp = Application.SldWorks

          Set swModeler = swApp.GetModeler

          Set swPart = swApp.ActiveDoc

          nPt(0) = 2

          nPt(1) = 0

          nPt(2) = 0

          vPt = nPt

          nPte(0) = 2

          nPte(1) = 0

          nPte(2) = 1

          vDir = nPte

          Set swCurve = swModeler.CreateLine(vPt, vDir)

          Set trimmed_curve = swCurve.CreateTrimmedCurve2(2, 0, 0, 2, 0, 1)

         Set swBody = trimmed_curve.CreateWireBody()

          retval = swBody.Display3(swPart, 255, swTempBodySelectable)

          Stop

      End Sub

       

      I've tried every combination that I can think of, but it doesn't seem to be doing anything. what I really want is a temporary line that is:

       

      K is some constant.

      Start (x, y, & z) End (x, y, & z+k)

       

      I have tried a trim that is effectively start (x-k, y-k,z-k) end (x+k, y+k, z+k), and that also errors out, when x, y, z are not all equal.

       

      I feel like I have ready everything on the forums in regards to the trimmed_curve and in the API, and I feel like I should at least be able to the third option. Thoughts? Any help is appreciated.

        • Re: CreateTrimmedCurve2 Not Working Correctly?
          Cody Kirsch

          I just glanced at you code, but I'm not sure your using CreateLine properly but to be fair the API help on this method is rather poor. I'm looking at some of what I have and its been a long time since I have written it but I believe the Direction parameter is not the end point like the API help said but rather a direction vector. Try making vDir = {1,0,0} or what ever direction you want.

           

          Edit

          Just to fill the gap left by the API Help, Modeler:CreateLine creates an infinite line in the Direction given. so if you wanted to go though the origin in the x direction RootPoint = {0,0,0} and Direction = {1,0,0}. Then to make it 1m long starting at 1m from the origin use swCurve=swCurve.CreateTrimmedCurve2(1, 0, 0, 2, 0, 0)

          • Re: CreateTrimmedCurve2 Not Working Correctly?
            Jacob Corder

            ok.  here is what all this means.

             

            CreateLine( RootPointX, RootPointY, RootPointZ,  DirectionX, DirectionY, DirectionZ)

             

            rootpoint is the point where it starts.

             

            Direction is the x y z magnitude of the line.  since lines are infinite in length its just a go this direction.  solidworks normalizes this line after you create it (normalize means that the lenght = 1

            ( so the SquareRoot of ((directionx*Directionx)+(directionY*DirectionY)+(DirectionZ*DirectionZ)) = 1)

             

            the trimmed curve is the actual points you want it to trim at.

             

            so to make it easier use this Function

            Dim swApp As SldWorks.SldWorks

            Sub main()

             

             

            Set swApp = Application.SldWorks

            Dim SwPart As ModelDoc2

            Set SwPart = swApp.ActiveDoc

            Dim SwModeler As Modeler

            Set SwModeler = swApp.GetModeler

            Dim FromPoint(2) As Double

             

            FromPoint(0) = 0

            FromPoint(1) = 1

            FromPoint(2) = 2

            Dim ToPoint(2) As Double

             

            ToPoint(0) = 4

            ToPoint(1) = 5

            ToPoint(2) = 6

            Dim LineBody As Body2

            Set LineBody = CreateWireBodyFromLine(SwModeler, FromPoint, ToPoint)

            If Not LineBody Is Nothing Then

              

            Dim Shown As Boolean

             

            Shown = LineBody.Display3(SwPart, 255, swTempBodySelectable)

            If Shown Then

                Stop

            End If

            End If

            End Sub

             

             

            Function CreateWireBodyFromLine(ByVal SwModeler As Modeler, ByRef FromXYZ() As Double, ByRef ToXYZ() As Double) As Body2

             

             

                 Dim TrimmedCurve As Curve

             

             

                 Set TrimmedCurve = CreateTrimmedLine(SwModeler, FromXYZ, ToXYZ)

             

             

                 If Not TrimmedCurve Is Nothing Then

             

             

                     Set CreateWireBodyFromLine = TrimmedCurve.CreateWireBody

             

             

                 End If

             

             

            End Function

             

             

             

             

             

             

            Function CreateTrimmedLine(ByVal SwModeler As Modeler, ByRef FromXYZ() As Double, ByRef ToXYZ() As Double) As Curve

                Dim LineDirection() As Double

                LineDirection = GetDirection(FromXYZ, ToXYZ, True)

               

                Dim InfiniteLineCurve As Curve

             

             

                Set InfiniteLineCurve = SwModeler.CreateLine(FromXYZ, LineDirection)

             

             

                If Not InfiniteLineCurve Is Nothing Then

             

             

            Set CreateTrimmedLine = InfiniteLineCurve.CreateTrimmedCurve2(FromXYZ(0), FromXYZ(1), FromXYZ(2), ToXYZ(0), ToXYZ(1), ToXYZ(2))

             

             

             

             

             

            End If

             

             

            End Function

             

             

            Function GetDirection(ByRef FromPoint() As Double, ByRef ToPoint() As Double, ByVal Normalize As Boolean) As Double()

                Dim RetDbl(2) As Double

                For i = 0 To 2

                    RetDbl(i) = ToPoint(i) - FromPoint(i)

                Next

               

                If Normalize Then

                    Dim VectLen As Double

                    VectLen = Math.Sqr((RetDbl(0) * RetDbl(0)) + (RetDbl(1) * RetDbl(1)) + (RetDbl(2) * RetDbl(2)))

                    For i = 0 To 2

                        RetDbl(i) = RetDbl(i) / VectLen

                    Next

               

                End If

            GetDirection = RetDbl

            End Function

             

             

            now read through how this executes and you will understand more on how vectors and 3 dimensional points work

              • Re: CreateTrimmedCurve2 Not Working Correctly?
                Brian Smith

                Jacob,

                 

                I appreciate the explanation, and I follow what you are saying, but as written I can't get any lines to show using your code.

                 

                does the numbers you put in as default work for you?

                 

                Dim FromPoint(2) As Double

                FromPoint(0) = 0

                FromPoint(1) = 1

                FromPoint(2) = 2

                Dim ToPoint(2) As Double

                ToPoint(0) = 4

                ToPoint(1) = 5

                ToPoint(2) = 6

                 

                Is this maybe some bug for my version maybe?