9 Replies Latest reply on Jun 26, 2014 7:22 AM by Ryan Hatzung

    Macro selects point@origin instead of line endpoint

    Ryan Hatzung

      I'm trying to created a referance plane on the end point of a line, however when the point is to close to the part origin point@origin is selected instead of the endpoint.

       

      Here is the code I'm using:

      -------------------------------------------

          'CREATE PLANE AT START PNT

          blnstatus = objPart.Extension.SelectByID2("", "EXTSKETCHPOINT", FirstPntX, FirstPntY, FirstPntZ, True, 0, Nothing, 0)

          blnstatus = objPart.Extension.SelectByID2("", "EXTSKETCHSEGMENT", FirstPntX, FirstPntY, FirstPntZ, True, 1, Nothing, 0)

         

          Dim swRefPlane As SldWorks.RefPlane

          Set swRefPlane = objPart.FeatureManager.InsertRefPlane(4, 0, 2, 0, 0, 0)

          objPart.ClearSelection2 True

      --------------------------------------------

       

      Also could anyone explain how the Append, Mark, Callout, SelectOption, and Value do for the SelectByID2 Method?

        • Re: Macro selects point@origin instead of line endpoint
          Simon Turner

          Select the line first (using your second SelectByID2 statement).

          Then get the result:

          Dim mySeg as SketchSegment

          Dim selMgr as SelectionManager

          Set selMgr = objPart.SelectionManager

          Set mySeg = selMgr.GetSelectedObject6(1,-1)

           

          Now you can access the endpoints of the sketch segment and use SketchPoint.Select to select the one with the correct coordinates.

           

          Append = False means deselect anything else that might already be selected. True leaves them selected as well (like holding Ctrl down)

          Mark is an integer which tells SolidWorks which selections go into which fields when you use them in a command. Read the Help file to find out what marks you will need to set.

          You won't need to use Callout or SelectOption

          • Re: Macro selects point@origin instead of line endpoint
            Vijay Kumar

            Hi Ryan,

             

            First you tell clearly that,  you have to create a ref. plane at end point of a line

             

            But in your code you give the coordinate as FirstPntX,FirstPntY,FirstPntZ ..., is it end point or start point and also you have a comments on while starting the code that 'CREATE PLANE AT START PNT'

             

            ok,

             

            Now you can zoom that area using viewzoomTo2  and then select that point using your second selectByID2 (Sketchsegment)

            in first selectByID2 statement Append must be as "False" and in second selectByID2 it is "True".

            -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

            Second one you are willing to create the plane in Assembly document right, then you have to get the modelpoint first.

             

            By using selection manager get the point as sketchsegment and assigned it's points as variant, and get that sketch and Pass it to the below said function

             

            Public Function getmodelpoint(ByVal inputpt As Variant, ByVal skt As SldWorks.Sketch)

             

                Dim swMathUtil              As SldWorks.MathUtility

                Dim swXform                 As SldWorks.MathTransform

                Dim swMathPt                As SldWorks.MathPoint


                Set swsketch = skt
               
                Set swXform = swsketch.ModelToSketchTransform

                Set swXform = swXform.Inverse

                Set swMathUtil = SwApp.GetMathUtility

                Set swMathPt = swMathUtil.CreatePoint((inputpt))

                Set swMathPt = swMathPt.MultiplyTransform(swXform)
               
                pts = swMathPt.ArrayData
               
                getmodelpoint = pts

            Debug.Print "      Point (model)    = (" & swMathPt.ArrayData(0) * 1000# & ", " & swMathPt.ArrayData(1) * 1000# & ", " & swMathPt.ArrayData(2) * 1000# & ") mm"

             

            End Function

             

            after getting the modelpoint, now you can select the point exactly.

            • Re: Macro selects point@origin instead of line endpoint
              Ryan Hatzung

              Thanks for the posts Simon and Vijay. I realize that I didn't give enough information on my original post. I would like to post the Macro itself, but because it was one my company paid someone to develop I'm not sure they would approve.

               

              The Macro itself was made to take XYZ coordinates out of an excel sheet and sketch a 3D centerline using line segments, each segment end point connecting at a coordinate. No actual sketch points just line segments.

               

              What I have been doing since we got the Macro is add simple lines of code to help improve what the Macro does for my company, and for an excuse to learn/relearn programming.

               

              Simon, your advice seems to be the closest to what I want, I'm just coming to realize that to select entities through a macro is more complicated then I first imagined (sorry, no real training in SW Macro programming. Just programming classes in college).

              Could either of you direct me to information on selection, or what you mean by the SketchPoint.Select?

               

              Thanks for your help guys.

                • Re: Macro selects point@origin instead of line endpoint
                  Simon Turner

                  Hi Ryan,

                   

                  if you are drawing the sketch segment yourself, you should already have a pointer to it.

                  So, something like this:

                   

                  Dim mySeg as SketchSegment

                   

                  set mySeg = objPart.SketchManager.CreateLine(x1, y1, z1, x2, y2, z2)

                   

                  Now we need to select the start point. SketchSegment does not give you direct access to that, but SketchLine does, so we convert our sketch segment to a sketch line and then get the start point:

                   

                  Dim myLine as SketchLine

                  Dim myPt as SketchPoint

                   

                  Set myLine = mySeg

                  Set myPt = myLine.GetStartPoint2

                   

                  'Now select both:

                   

                  mySeg.Select4 False, Nothing

                  myPt.Select4 True, Nothing

                   

                  If you need to mark each one, you need to use a SelectData object:

                   

                  Dim mySelectData as SelectData

                   

                  Set mySelectData = objPart.SelectionManager.CreateSelectData

                  mySelectData.Mark = 0

                  mySeg.Select4 False, mySelectData

                   

                  mySelectData.Mark = 1

                  myPt.Select4 True, mySelectData

                   

                   

                  Cheers

                • Re: Macro selects point@origin instead of line endpoint
                  Vijay Kumar

                  Hi Ryan,

                   

                  Kindly go through the code for creating a referenceplane.

                   

                  ---------------------------------------------------------

                  Dim swApp As SldWorks.SldWorks
                  Dim objPart As SldWorks.ModelDoc2
                  Dim sktseg As SldWorks.SketchSegment
                  Dim myline As SldWorks.SketchLine
                  Dim mypt As SldWorks.SketchPoint
                  Dim swRefPlane As SldWorks.refplane
                  Dim blnstatus As Boolean
                  Dim FirstPntX1, FirstPntX2, FirstPntY1, FirstPntY2, FirstPntZ1, FirstPntZ2 As Double

                   

                   

                  Sub main()

                   

                  Set swApp = Application.SldWorks
                  Set objPart = swApp.ActiveDoc
                  objPart.Insert3DSketch
                  FirstPntX1 = 2: FirstPntX2 = 8: FirstPntY1 = 1: FirstPntY2 = 10: FirstPntZ1 = 0: FirstPntZ2 = 5
                  Set sktseg = objPart.SketchManager.CreateLine(FirstPntX1, FirstPntY1, FirstPntZ1, FirstPntX2, FirstPntY2, FirstPntZ2)
                  objPart.Insert3DSketch
                  objPart.ClearSelection2 True
                  objPart.ViewZoomtofit2
                  blnstatus = objPart.Extension.SelectByID2("", "EXTSKETCHSEGMENT", FirstPntX1, FirstPntY1, FirstPntZ1, False, 0, Nothing, 0)
                  blnstatus = objPart.Extension.SelectByID2("", "EXTSKETCHPOINT", FirstPntX1, FirstPntY1, FirstPntZ1, True, 1, Nothing, 0)

                  Set swRefPlane = objPart.FeatureManager.InsertRefPlane(2, 0, 4, 0, 0, 0)

                   

                  End sub

                  --------------------------------------------------------

                  • Re: Macro selects point@origin instead of line endpoint
                    Ryan Hatzung

                    Sorry for the late reply, I've been trying to do more learning into the Selection Subject matter. Simon, it would seem that using the Select4 method is the way I need to go. I am able to use this method to sketch a line between Coordinate points, get the start point data, select the start point regardless of what is in front of the start point on the screen, then fix the start point in place.

                    -----------------------------

                        Do Until objWorksheet.Cells(intRow + 1, intXColumn) = "" 'go until we don't have any more X coordinates

                            'get the endpoint

                            dblX2 = objWorksheet.Cells(intRow + 1, intXColumn) * dblConversion

                            dblY2 = objWorksheet.Cells(intRow + 1, intYColumn) * dblConversion

                            dblZ2 = objWorksheet.Cells(intRow + 1, intZColumn) * dblConversion

                            dblRadius = objWorksheet.Cells(intRow + 1, intRadiusColumn) * dblConversion

                            'insert line

                            Set clLine = objPart.SketchManager.CreateLine(dblX1, dblY1, dblZ1, dblX2, dblY2, dblZ2)

                            Set clPointStrt = clLine.GetStartPoint2

                           

                            clPointStrt.Select4 False, Nothing

                            objPart.SketchAddConstraints "sgFIXED" 'FIX PNT - RHATZUNG 5-13-14

                               

                            'bring the end to the begining

                            dblX1 = dblX2

                            dblY1 = dblY2

                            dblZ1 = dblZ2

                            dblRadius = 0#

                           

                            intRow = intRow + 1

                            intCoordCnt = intCoordCnt + 1

                        Loop

                       

                        Set clPointStrt = clLine.GetEndPoint2

                        clPointStrt.Select4 False, Nothing

                        objPart.SketchAddConstraints "sgFIXED" 'FIX PNT - RHATZUNG 5-13-14

                    ---------------------------------------------

                     

                    Now my next step is to go back through the points and create a fillet to make a bend on the cl

                     

                    -------------------------------------------------

                        intRow = intFindStartRow

                        Do Until objWorksheet.Cells(intRow, intXColumn) = ""

                            'get the endpoint

                            dblX1 = objWorksheet.Cells(intRow, intXColumn) * dblConversion

                            dblY1 = objWorksheet.Cells(intRow, intYColumn) * dblConversion

                            dblZ1 = objWorksheet.Cells(intRow, intZColumn) * dblConversion

                            dblRadius = objWorksheet.Cells(intRow, intRadiusColumn) * dblConversion

                           

                            'if a radius is listed for this point, add it

                            If dblRadius > 0 Then

                                blnstatus = objPart.Extension.SelectByID2("", swSelectType_e.swSelSKETCHPOINTS, dblX1, dblY1, dblZ1, False, 0, Nothing, 0)

                                If blnstatus = 1 Then

                                    objPart.SketchManager.CreateFillet dblRadius, 1 'create a radius at selected point

                                End If

                            End If

                                   

                            intRow = intRow + 1

                        Loop

                    ----------------------------------------

                     

                    Since I kept replacing the line data and point data in the sketch line loop I'm trying to figure out how to rereferance each line or point so that I can use the select 4 method for the fillet creation. The number of points is not a set number on each centerline we are drawing. The only thing that I can think of is if possible setting each point data to an array. However I have no idea if that is possible.

                     

                    Thanks again guys, I am learning alot through this trial and error.