19 Replies Latest reply on Apr 25, 2013 4:44 AM by Matt Nixon

    Importing objects using a macro

    Matt Nixon

      I'm sure this is a very basic question (please excuse my ignorance, I am not a regular solidworks user), but I haven't been able to find any information on it (quite possibly searching the wrong terms).

       

      Currently, I'm importing a list of 3D coordinates from a text file using a macro somebody had posted on another forum (below). What I would actually like to do is have spheres (all same diameter), with their centre at each coordinate point. I'm guessing I could do this either via altering the macro to place a sphere at each coordinate, rather than just a point, or by changing the points to spheres once imported?

       

      If anybody could suggest an edit to the macro script or otherwise explain how to achive this, that would be great!

       

       

      The macro script:

       

      Dim swApp As Object

      Sub main()

       

      Set swApp = Application.SldWorks

      Set Part = swApp.ActiveDoc

      swApp.ActiveDoc.ActiveView.FrameState = 1

      Dim skPoint As Object

       

      Open "D:\Spiloxene\Modelling RCP\March 13\22 15^3 box\xyz.txt" For Input As #1

      Part.SketchManager.Insert3DSketch True

      Do While Not EOF(1)

      Input #1, X, Y, Z

      Set skPoint = Part.SketchManager.CreatePoint(X, Y, Z)

      Loop

      Close #1

       

      Part.ShowNamedView2 "*Iscmetric", 7

      Part.ViewZoomtofit2

      End Sub


        • Re: Importing objects using a macro
          Jerry Steiger

          Matt,

           

          You will probably get a better answer if your move this to the API forum. I'm not a macro person, so I can't help with how to turn your points into spheres in the macro. As a separate operation, you might be able to use a Sketch Driven Pattern to copy a seed sphere, but I think it is limited to 2D Sketch points.

           

          Jerry S.

          • Re: Importing objects using a macro
            Patrick O'Hern

            You're best option will probably be to create a sphere at the coords of the first point (Sketch half a circle and then revolve).  The do a Sketch Driven Pattern (it will work with 3D sketches).

            • Re: Importing objects using a macro
              Patrick O'Hern

              Try this:  You can specify your sphere radius on the line in bold

               

              Option Explicit

                  Dim swApp As SldWorks.SldWorks
                  Dim swModelDoc As SldWorks.ModelDoc2
                 
              Sub main()
                  Dim skPoint As SldWorks.SketchPoint
                  Dim X As Double, Y As Double, Z As Double
                 
                  Set swApp = Application.SldWorks
                  Set swModelDoc = swApp.ActiveDoc
                  swApp.ActiveDoc.ActiveView.FrameState = 1
                  
                  Open "D:\Spiloxene\Modelling RCP\March 13\22 15^3 box\xyz.txt" For Input As #1
                  swModelDoc.SketchManager.Insert3DSketch True
                  Do While Not EOF(1)
                  Input #1, X, Y, Z
                  Set skPoint = swModelDoc.SketchManager.CreatePoint(X, Y, Z)
                  Loop
                  Close #1
                 
                  Call AddSpheres(X, Y, Z)
                  
                  swModelDoc.ShowNamedView2 "*Isometric", 7
                  swModelDoc.ViewZoomtofit2

              End Sub


              Sub AddSpheres(X As Double, Y As Double, Z As Double)
                  Dim swFeat As SldWorks.Feature
                  Dim swFeatMgr As SldWorks.FeatureManager
                  Dim skSegment As SldWorks.SketchSegment
                  Dim swDispDim As SldWorks.DisplayDimension
                  Dim bStatus As Boolean
                  Const dblRadius As Double = 0.01 'Radius of sphere in meters
                 
                  Set swFeatMgr = swModelDoc.FeatureManager
                 
              'Select "Front" plane
                  bStatus = swModelDoc.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, _
                      False, 0, Nothing, 0)
              'Insert new sketch
                  swModelDoc.SketchManager.InsertSketch True
                 
              'Create arc
                  Set skSegment = swModelDoc.SketchManager.CreateArc(0, 0, 0, 0, dblRadius, 0, _
                      0, -dblRadius, 0, -1)
              'Add vertical line
                  Set skSegment = swModelDoc.SketchManager.CreateLine(0, dblRadius, 0, _
                      0, -dblRadius, 0)
              'Add arc center point to current selection
                  bStatus = swModelDoc.Extension.SelectByID2("", "SKETCHPOINT", 0, 0, 0, True, 0, Nothing, 0)
              'Add "Midpoint" relation between arc center point and vertical center line
                  swModelDoc.SketchAddConstraints "sgATMIDDLE"
                 
              'Clear selections
                  swModelDoc.ClearSelection2 True
              'Select vertical line
                  bStatus = swModelDoc.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0, 0, _
                      True, 0, Nothing, 0)
              'Add dimension
                  Set swDispDim = swModelDoc.AddDimension2(-dblRadius, 0, 0)
              'Exit sketch
                  swModelDoc.InsertSketch2 True

              'Add Revolve feature
                  bStatus = swModelDoc.Extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, _
                      False, 0, Nothing, 0)
                  bStatus = swModelDoc.Extension.SelectByID2("Line1@Sketch1", "SKETCHSEGMENT", _
                      0, 0, 0, True, 4, Nothing, 0)
                  Set swFeat = swModelDoc.FeatureManager.FeatureRevolve2(True, True, False, False, False, _
                      False, 0, 0, 6.28318530718, 0, False, False, 0.01, 0.01, 0, 0, 0, False, True, True)
               
              'Clear selections
                  swModelDoc.ClearSelection2 True
              'Get the body
                  bStatus = swModelDoc.Extension.SelectByID2("Revolve1", "SOLIDBODY", 0, 0, 0, _
                      True, 1, Nothing, 0)
              'Move the body to the first point
                  Set swFeat = swFeatMgr.InsertMoveCopyBody2(X, Y, Z, 0, 0, 0, 0, 0, 0, 0, False, 1)

              'Clear selections
                  swModelDoc.ClearSelection2 True
              'Get the body and 3D sketch
                  bStatus = swModelDoc.Extension.SelectByID2("Body-Move/Copy1", "SOLIDBODY", 0, 0, 0, _
                      True, 256, Nothing, 0)
                  bStatus = swModelDoc.Extension.SelectByID2("3DSketch1", "SKETCH", 0, 0, 0, _
                      True, 64, Nothing, 0)
              'Pattern the body
                  Set swFeat = swFeatMgr.FeatureSketchDrivenPattern(True, False)
              End Sub

              • Re: Importing objects using a macro
                Patrick O'Hern

                Please note that the SW API only uses METERS for distances.  If your coordinates are in something other than meters, you'll need to modify the macro to convert them.

                • Re: Importing objects using a macro
                  Matt Nixon

                  Thank you for the suggestion Jerry, and for the macro script Patrick.

                   

                  When the script finished running, I get a dialog box pop up titled 'Modify' then otpions to modify 'D1@Sketch1' for different thicknesses. I don't know what this is... If I click the tick icon 'Save the current value and exit the dialog' then I end up with a 3D sketch showing my coordinate points (rather than spheres). Am I missing the point or doing something wrong?

                   

                  Many thanks,

                   

                  Matt

                    • Re: Importing objects using a macro
                      Patrick O'Hern

                      Are you starting with a new file or running this in a file with existing geometry/sketches?

                       

                      If you are working with a new file, you might try commenting out the line that adds a dimension:

                      'Set swDispDim = swModelDoc.AddDimension2(-dblRadius, 0, 0)'

                       

                      You would need to manually add the dimension if you wanted the sketch to be fully defined.

                      • Re: Importing objects using a macro
                        Patrick O'Hern

                        Check your System Options.  In the General Tab, the very first option is "Input dimension value".  Try unchecking this and see if it helps.  (This will allow the dimension to be added without having to have a manual input)

                         

                        Also, depending on the spacing of your 3D points, you may just need to zoom in on one of them to see the spheres, as they may appear very small.  They are currently being drawn at a diameter of 20mm (.01 Meter radius)

                         

                        Message was edited by: Patrick O\'Hern

                      • Re: Importing objects using a macro
                        Matt Nixon

                        I'm starting with a new file. I'm not sure what you mean by "You would need to manually add the dimension if you wanted the sketch to be fully defined." if the coorinate points are 3-dimensional then would the sketch not automatically be 3D also?

                         

                        Unticking the "input dimension value" box, but this didn't appear to have any effect...

                         

                        I did alter the sphere diameters as well - they are a comparable size to the spacing between them, but all I can see is the points?

                         

                        Is there anything else I might try/change?

                         

                        Thanks again,

                         

                        Matt

                        • Re: Importing objects using a macro
                          Matt Nixon

                          Currently having more general issues with solidworks (I think it's to do with our license server) so I haven't been able to take the screenshot... Hopefully it will be fixed tomorrow and I will be able to do this.

                           

                          I have attached a text file of the coordinates. I've multiplied their values by 1x10^9 as it didn't like importing even just the points when they were on the sub micron scale. As the coordinates currently stand, their radii should be 200m.

                            • Re: Importing objects using a macro
                              Patrick O'Hern

                              Your file didn't get attached.

                              • Re: Importing objects using a macro
                                Patrick O'Hern

                                Ok, the problem was that your points were too close together for the macro to work.  When you scaled them up, you went too large and surpassed the physical size limits of the software (500m in each direction).  I modified the macro to allow the points to be added in their correct location (at the sub micron scale), but then I ran into a problem with the physical size of the sphere.  With a solid sphere, I can't get the revolve to work under 500 nanometer radius.  I can, however, get down to 50nm radius by using a surface revolve, rather than a solid.

                                 

                                I have to modify the code slightly to work with the surface, so let me know which you would prefer to have and I'll post the updated code.

                                  • Re: Importing objects using a macro
                                    Matt Nixon

                                    Right ok then. I think a solid is definitely preferable to a surface as I will be taking cross-sections through the structure and I'm not sure this would do want I'm after as well with a surface. Would the revolve work if the spheres were solids but scaled up in size by, say, a factor of 10?

                                      • Re: Importing objects using a macro
                                        Patrick O'Hern

                                        I have added a new constant to this version (dblMulti) that acts as a mulitplier to your inputs.  You can use it to adjust your scale rather than manually updating your text files.  It is currently scaling up by 10, if you don't want to use it, just set it to 1.

                                         

                                        I have also set the sphere radius to the smallest number that I could get to work.  Feel free to try adjusting it on your own.

                                         

                                        I tested this using the text file you attached, but I modified the values back to what I assume were the original numbers (divided by 1x10^9).  Everything looks ok to me with the macro mulitplier set at 10.

                                         

                                        Option Explicit

                                            Dim swApp As SldWorks.SldWorks
                                            Dim swModelDoc As SldWorks.ModelDoc2
                                           
                                        Sub main()
                                            Dim skPoint As SldWorks.SketchPoint
                                            Dim X As Double, Y As Double, Z As Double
                                           Const dblMulti As Double = 10
                                           
                                            Set swApp = Application.SldWorks
                                            Set swModelDoc = swApp.ActiveDoc
                                            swModelDoc.ViewZoomTo2 0, 0, 0, 0.0000001, 0.0000001, 0.0000001
                                            
                                            Open "D:\Spiloxene\Modelling RCP\March 13\22 15^3 box\xyz.txt" For Input As #1
                                            swModelDoc.SketchManager.Insert3DSketch True
                                            Do While Not EOF(1)
                                                Input #1, X, Y, Z
                                                Set skPoint = swModelDoc.SketchManager.CreatePoint(X * dblMulti, Y * dblMulti, Z * dblMulti)
                                            Loop
                                          
                                        'Close and hide 3D sketch
                                            swModelDoc.SketchManager.InsertSketch True
                                            swModelDoc.BlankSketch
                                           
                                            Close #1
                                           
                                            Call AddSpheres(X, Y, Z)
                                            
                                            swModelDoc.ShowNamedView2 "*Isometric", 7
                                            swModelDoc.ViewZoomtofit2

                                        End Sub

                                        Sub AddSpheres(X As Double, Y As Double, Z As Double)
                                            Dim swFeat As SldWorks.Feature
                                            Dim swFeatMgr As SldWorks.FeatureManager
                                            Dim skSegment As SldWorks.SketchSegment
                                            Dim swDispDim As SldWorks.DisplayDimension
                                            Dim bStatus As Boolean
                                            Const dblRadius As Double = 0.00000032 'Radius of sphere in meters

                                            Set swFeatMgr = swModelDoc.FeatureManager
                                           
                                        'Select "Front" plane
                                            bStatus = swModelDoc.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, _
                                                False, 0, Nothing, 0)
                                        'Insert new sketch
                                            swModelDoc.SketchManager.InsertSketch True
                                           
                                        'Create arc
                                            Set skSegment = swModelDoc.SketchManager.CreateArc(0, 0, 0, 0, dblRadius, 0, _
                                                0, -dblRadius, 0, -1)
                                        'Add vertical line
                                            Set skSegment = swModelDoc.SketchManager.CreateLine(0, dblRadius, 0, _
                                                0, -dblRadius, 0)
                                        'Add arc center point to current selection
                                            bStatus = swModelDoc.Extension.SelectByID2("", "SKETCHPOINT", 0, 0, 0, True, 0, Nothing, 0)
                                        'Add "Midpoint" relation between arc center point and vertical center line
                                            swModelDoc.SketchAddConstraints "sgATMIDDLE"
                                           
                                        'Clear selections
                                            swModelDoc.ClearSelection2 True
                                        'Select vertical line
                                            bStatus = swModelDoc.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 0, 0, 0, _
                                                True, 0, Nothing, 0)
                                        'Add dimension
                                        '    Set swDispDim = swModelDoc.AddDimension2(-dblRadius, 0, 0)
                                        'Exit sketch
                                            swModelDoc.InsertSketch2 True

                                        'Add Revolve feature
                                            bStatus = swModelDoc.Extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, _
                                                False, 0, Nothing, 0)
                                            bStatus = swModelDoc.Extension.SelectByID2("Line1@Sketch1", "SKETCHSEGMENT", _
                                                0, 0, 0, True, 4, Nothing, 0)
                                            Set swFeat = swModelDoc.FeatureManager.FeatureRevolve2(True, True, False, False, False, _
                                                False, 0, 0, 6.28318530718, 0, False, False, 0.01, 0.01, 0, 0, 0, False, True, True)
                                         
                                        'Clear selections
                                            swModelDoc.ClearSelection2 True
                                        'Get the body
                                            bStatus = swModelDoc.Extension.SelectByID2("Revolve1", "SOLIDBODY", 0, 0, 0, _
                                                True, 1, Nothing, 0)
                                        'Move the body to the first point
                                            Set swFeat = swFeatMgr.InsertMoveCopyBody2(X, Y, Z, 0, 0, 0, 0, 0, 0, 0, False, 1)

                                        'Clear selections
                                            swModelDoc.ClearSelection2 True
                                        'Get the body and 3D sketch
                                            bStatus = swModelDoc.Extension.SelectByID2("Body-Move/Copy1", "SOLIDBODY", 0, 0, 0, _
                                                True, 256, Nothing, 0)
                                            bStatus = swModelDoc.Extension.SelectByID2("3DSketch1", "SKETCH", 0, 0, 0, _
                                                True, 64, Nothing, 0)
                                        'Pattern the body
                                            Set swFeat = swFeatMgr.FeatureSketchDrivenPattern(True, False)
                                        End Sub

                                         

                                        Message was edited by: Patrick O\'Hern

                                  • Re: Importing objects using a macro
                                    Matt Nixon

                                    OK, so I'm guessing I must be doing something wrong if the macro is working for you. If you could see a flaw in what I'm doing that would be very usefull.

                                     

                                    All I'm doing is opening solidworks and creating a new solidworks part. From there I am running the macro (anything else I need to do first?)

                                     

                                    The first result of the macro was what appeared to be only one sphere (though in the feature tree, there were 1954 solid bodies listed). Running the macro with a new file with the "input dimension value" box ticked gave me the game dialogue box as before (titled 'Modify' with options to modify 'D1@Sketch1' for different thicknesses).

                                     

                                    Since this I seem to be unable to repeat the original result. I've provided a link to a pdf with images to try and show my result better than I could describe it.

                                     

                                    https://www.dropbox.com/s/9q3k50vvi5oeyxj/macro_results.pdf

                                     

                                    Thanks again,

                                     

                                    Matt

                                      • Re: Importing objects using a macro
                                        Patrick O'Hern

                                        Comment out the line adding a dimension "Set swDispDim = swModelDoc.AddDimension2(-dblRadius, 0, 0)" (Put a single quote mark ' at the beginning of the line).  That should get rid of the dimension box popping up.

                                         

                                        As far as why you only see one sphere, that is weird.  The macro appears to have run correctly, as it shows all the bodies were created.  Is it possible the bodies are hidden?

                                         

                                        Try making the sphere slightly larger, or adjusting the "dblMulti" scale.