10 Replies Latest reply on Feb 26, 2014 3:04 AM by Yuvaraj Kasi

    Coordinate Sytem - Orentation with Line selection

    Arun M. A

      Hi All,

       

      I am trying to findout a way to Create / Modified Cooridinate system Orentation with Line slection. Can anyone has any idea on this.

       

      I tried (get from API help) to use as below but i am not able to succeed.

       

      ' Assume a vertex was selected for the origin

      Set so(0) = swSelMgr.GetSelectedObject6(1, -1)

      ' Assume one entity (point, line/edge, plane/face) was selected for the x axis

      Set sx(0) = swSelMgr.GetSelectedObject6(2, -1)

      ' Assume two entities (point, line/edge, plane/face) were selected for the y axis

      Set sy(0) = swSelMgr.GetSelectedObject6(3, -1)

      Set sy(1) = swSelMgr.GetSelectedObject6(4, -1)

      ' Try getting additional selected entities for the z axis

      Set sz(0) = swSelMgr.GetSelectedObject6(5, -1)

      Set sz(1) = swSelMgr.GetSelectedObject6(6, -1)

       

      vo = so

      vx = sx

      vy = sy

      vz = sz

       

      swModel.ClearSelection2 True

       

      ' Create coordinate system

      Set swFeat = swFeatMgr.CreateCoordinateSystem(so(0), (vx), (vy), (vz))

        • Re: Coordinate Sytem - Orentation with Line selection
          Vishaldeep Singh

          Sir i am working in solidworks 2012

          the code you have posted is giving an error

          error is

           

          compile error:

          cant find project or library.

            • Re: Coordinate Sytem - Orentation with Line selection
              Yuvaraj Kasi

              Hi Vishal,

              check if you have checked below options at Tools==>references

              tools--references.jpg

               

              now record a new macro and replace the etire content with below code and try now

               

               

              ' **************************************************************************************************************************************
              ' * CS Point will be created at Point2@Sketch1 and X-Axis flipped and aligned with Line1@Sketch1 and Y-axis aligned with Line2@Sketch1 *
              ' **************************************************************************************************************************************
              Option Explicit

              Dim swApp As SldWorks.SldWorks
              Dim Part As SldWorks.ModelDoc2
              Dim swSelMgr As SldWorks.SelectionMgr
              Dim swFeat As SldWorks.Feature
              Dim swFeatMgr As SldWorks.FeatureManager
              Dim cSysData As SldWorks.CoordinateSystemFeatureData
              Dim component As SldWorks.Component2
              Dim boolstatus As Boolean
              Dim x1, y1, z1 As Double
              Dim skPoint As SketchPoint
              Dim vo As Variant
              Dim vx As Variant
              Dim vy As Variant
              Dim vz As Variant
              Dim so(0) As Object
              Dim sx(1) As Object
              Dim sy(1) As Object
              Dim sz(1) As Object
              Dim rename1, rename2 As Boolean


              Sub main()

              Set swApp = Application.SldWorks
              Set Part = swApp.ActiveDoc
              Set swSelMgr = Part.SelectionManager
              Set swFeatMgr = Part.FeatureManager

               

              'This part assigns x1,y1,z1 for XYZ cordinates to replace the variable numerical value.

                  Part.ClearSelection2 True
                      boolstatus = Part.Extension.SelectByID2("Point2@Sketch1", "EXTSKETCHPOINT", 0, 0, 0, False, 0, Nothing, 0)
                      Set skPoint = swSelMgr.GetSelectedObject6(1, -1)

                      x1 = skPoint.X
                      y1 = skPoint.Y
                      z1 = skPoint.Z
                 
                  Part.ClearSelection2 True

              'All reference features to create a Coordinate System Point are selected here
                      boolstatus = Part.Extension.SelectByID2("Point2@Sketch1", "EXTSKETCHPOINT", x1, y1, z1, False, 1, Nothing, 0)
                      boolstatus = Part.Extension.SelectByID2("Line1@Sketch1", "EXTSKETCHSEGMENT", x1, y1, z1, True, 1, Nothing, 0)
                      boolstatus = Part.Extension.SelectByID2("Line2@Sketch1", "EXTSKETCHSEGMENT", x1, y1, z1, True, 1, Nothing, 0)

              '********************************
              'HOW THE ORIENTATION OF COORDINATESYSTEM POINT CAN BE CONTROLLED?
              'Selected items will be assigned to So(0), sx(0), sy(0), sy(1), sz(0), and sz(1) in the order of selection.
              'For example, Point2 will be assigned to so(0), Line1 will be assigned to sx(0), Line2 will be assigned to sy(0).
              '               if you comment the line for sx(0), Line1 will be assigned to sy(0), and Line2 will be assigned to sy(1).
              '********************************


              ' Assume a vertex was selected for the origin
                  Set so(0) = swSelMgr.GetSelectedObject6(1, -1)

              ' Assume one entity (point, line/edge, plane/face) was selected for the x axis
                  Set sx(0) = swSelMgr.GetSelectedObject6(2, -1)

              ' Assume two entities (point, line/edge, plane/face) were selected for the y axis. (sy(1) not applicable in this example)
                  Set sy(0) = swSelMgr.GetSelectedObject6(3, -1)
                  Set sy(1) = swSelMgr.GetSelectedObject6(4, -1)

              ' Try getting additional selected entities for the z axis (not applicable in this example)
                  Set sz(0) = swSelMgr.GetSelectedObject6(5, -1)
                  Set sz(1) = swSelMgr.GetSelectedObject6(6, -1)

              'This section creates arrays (vo,vx,vy,vz) containing the reference entities required
                      vo = so
                      vx = sx
                      vy = sy
                      vz = sz

                      Part.ClearSelection2 True

              ' Create coordinate system
                  Set swFeat = swFeatMgr.CreateCoordinateSystem(so(0), (vx), (vy), (vz))

              ' Flip X/Y/Z axis
                  Set cSysData = swFeat.GetDefinition
                  boolstatus = cSysData.AccessSelections(Part, component)
                  cSysData.XFlipped = True
                  boolstatus = swFeat.ModifyDefinition(cSysData, Part, Nothing)
                  cSysData.ReleaseSelectionAccess

              ' Rename the CS point as desired
                  rename1 = Part.GetEntityName(swFeat)
                  rename2 = Part.SetEntityName(swFeat, "NewCsPoint")


              Part.ClearSelection2 True

              End Sub

              ' **************************************************************************************************************************************