7 Replies Latest reply on Aug 22, 2018 1:01 PM by Josh Brady

    Create cylinders in 3D space based on XYZ location of a text file

    Aysn Gvn

      Dear all,

       

      I would like to create spheres in 3D space based on the XYZ location read from a text file. As I have heard, the easiest method would be creating the spheres at the origin and then moving them to the XYZ location for each case. However, after creating the spheres, I don't know how to move each sphere to the location.

       

      I would be thankful if you help me.

        • Re: Create cylinders in 3D space based on XYZ location of a text file
          Nilesh Patel

          How are you creating your spheres? Could you post your codes?

            • Re: Create cylinders in 3D space based on XYZ location of a text file
              Aysn Gvn

              Dear Nilesh, I uploaded the sphere macro file.

                • Re: Create cylinders in 3D space based on XYZ location of a text file
                  Josh Brady

                  Aysn Gvn wrote:

                   

                  Dear Nilesh, I uploaded the sphere macro file.

                  I looked at your code.  I don't think your approach is very efficient.  I don't know who told you to model the sphere at the origin and then move it.  I can't see that being useful. 

                   

                  Follow the link I posted.  Read the posts. Download the macro that I wrote that works.  If you so desire, modify the code to read a text file instead of Excel.  Or, just paste your XYZ data to Excel and run the macro.

                    • Re: Create cylinders in 3D space based on XYZ location of a text file
                      Aysn Gvn

                      Dear Josh,

                       

                      Thank you very much. I will try your code and let you know if there is any problem.

                        • Re: Create cylinders in 3D space based on XYZ location of a text file
                          Josh Brady

                          Does this PDF look anything like the result you were expecting?

                           

                          Here is the code to generate that model.  To use it, you will need to put your XYZ values into Excel and add a column for the desired sphere diameter.  Diameter must be in Column A, then X,Y,Z in column B,C,D.

                           

                          I don't know what units your data is in.  This code currently assumes that your data is in meters.  If your data is in mm or any other unit, you need to change the constant "SF" to a different value.

                           

                          Option Explicit
                            
                            
                          Const REPORTINTERVAL As Long = 10
                          Const SPHERESPERFILE As Long = 500
                          Const MAKESOLIDS As Boolean = True
                          Const XLPATH As String = "C:\Users\jbrady\Desktop\Folderize\XYZ_points.xlsx"
                          Const SphereMax As Double = 0.05 '(50mm)
                          Const SF As Double = 1 'Looks like data is already in meters? 0.001 'multiplier to change mm to m
                          Const BoxSize As Double = 1 ' (1m)
                          Const NumSpheres As Long = 50
                          Dim xlApp As Object
                          Dim xlBook As Object
                          Dim xlSht As Object
                          Dim swPane As SldWorks.StatusBarPane
                              
                              
                              
                          Sub main()
                              
                              Dim swApp As SldWorks.SldWorks
                              Dim swDoc As ModelDoc2
                              Dim swBody As Body2
                              Dim vBods As Variant
                              Dim swPart As PartDoc
                              Dim swModeler As Modeler
                              Dim swFeat As Feature
                              
                              Set swApp = Application.SldWorks
                              Set swDoc = swApp.ActiveDoc
                              Set swPart = swDoc
                              Set swModeler = swApp.GetModeler
                              Set swPane = swApp.Frame.GetStatusBarPane
                              
                              Dim swSurf As Surface
                              Dim swSurfPara As SldWorks.SurfaceParameterizationData
                              Dim Arr3Doubles(3) As Double
                              Dim UVRange(3) As Double
                              Dim vCenter As Variant
                              Dim vAxis As Variant
                              Dim vRefDir As Variant
                              Dim i As Long
                              Dim StopWatch As Double
                              Dim DoEmAll As Long
                              Dim LastSaved As Long
                                
                              'Create XL
                              Set xlApp = CreateObject("Excel.Application")
                              Set xlBook = xlApp.Workbooks.Open(XLPATH)
                              Set xlSht = xlApp.ActiveSheet
                                  
                              
                              Randomize
                              
                              DoEmAll = MsgBox("Do all spheres?  Choosing ""No"" will only do first " & NumSpheres & _
                                      vbCrLf & "Window will hide to improve performance.", vbYesNo)
                                  
                              StopWatch = Timer
                              i = 1
                              swDoc.Visible = False
                              LastSaved = 1
                              Do While xlSht.Cells(i, 1).Value <> ""
                                  Arr3Doubles(0) = xlSht.Cells(i, 2).Value * SF: Arr3Doubles(1) = xlSht.Cells(i, 3).Value * SF: Arr3Doubles(2) = xlSht.Cells(i, 4).Value * SF
                                  vCenter = Arr3Doubles
                                  Arr3Doubles(0) = 0: Arr3Doubles(1) = 0: Arr3Doubles(2) = 1
                                  vAxis = Arr3Doubles
                                  Arr3Doubles(0) = 0: Arr3Doubles(1) = 1: Arr3Doubles(2) = 0
                                  vRefDir = Arr3Doubles
                                      
                                    'post states file contains diameter. Therefore divide by 2 in sphere creation for radius.
                                  Set swSurf = swModeler.CreateSphericalSurface2(vCenter, vAxis, vRefDir, xlSht.Cells(i, 1).Value * SF / 2)
                                  
                                  Set swSurfPara = swSurf.Parameterization2
                                  
                                  UVRange(0) = swSurfPara.UMin
                                  UVRange(1) = swSurfPara.UMax
                                  UVRange(2) = swSurfPara.VMin
                                  UVRange(3) = swSurfPara.VMax
                                  
                                  Set swBody = swModeler.CreateSheetFromSurface(swSurf, UVRange)
                                  
                                  Set swFeat = swPart.CreateFeatureFromBody3(swBody, False, 3) 'swCreateFeatureBodyCheck + swCreateFeatureBodySimplify)
                                  
                                  swFeat.Name = "Sphere_" & i
                                    
                                  If i Mod REPORTINTERVAL = 0 Then
                                      swPane.Text = "Spheres created: " & i & " (Updates every " & REPORTINTERVAL & ")"
                                  End If
                                    
                                  If i Mod SPHERESPERFILE = 0 Then
                                      If MAKESOLIDS Then
                                          swDoc.ImportDiagnosis True, False, True, 0
                                      End If
                                      swDoc.Extension.SaveAs swApp.GetCurrentMacroPathFolder & "\Spheres" & LastSaved & "-" & i & ".sldprt", 0, 8 + 1, Nothing, Empty, Empty
                                      LastSaved = i + 1
                                      Set swDoc = Nothing
                                      swApp.CloseAllDocuments True
                                      Set swDoc = swApp.NewPart
                                      Set swPart = swDoc
                                      swDoc.Visible = False
                                  End If
                                  i = i + 1
                                  If (DoEmAll = vbNo) And (i > NumSpheres) Then Exit Do
                              Loop
                              If MAKESOLIDS Then
                                  swDoc.ImportDiagnosis True, False, True, 0
                              End If
                              swDoc.Extension.SaveAs swApp.GetCurrentMacroPathFolder & "\Spheres" & LastSaved & "-" & i - 1 & ".sldprt", 0, 8 + 1, Nothing, Empty, Empty
                              Set xlSht = Nothing
                               Set xlBook = Nothing
                               xlApp.Quit
                               Set xlApp = Nothing
                               Set swPane = Nothing
                            swDoc.Visible = True
                            swDoc.ViewZoomtofit
                              MsgBox "Finished in " & Round(Timer - StopWatch, 2) & " seconds."
                              
                          End Sub
                          
                  • Re: Create cylinders in 3D space based on XYZ location of a text file
                    Josh Brady

                    Please see:  Creating spheres based on location cloud

                     

                    If that does not fit your need, please let us know what you need to be different.

                    • Re: Create cylinders in 3D space based on XYZ location of a text file
                      Josh Brady

                      One might also ask... The title of this post says "cylinders".  The body of the post and your code seem to indicate spheres.  Which do you want?  I also note that you asked about cylinders more specifically in some earlier post.  Are the spheres a substitute for the cylinders, or do you ultimately want cylinders?  Cylinders can be created directly from the modeler as well.