22 Replies Latest reply on Dec 13, 2012 2:19 PM by mehrdad keyno

    Exporting x,y,z corrdinates

    Ed Morris
      Dear Experts,

      I have a series of points located in a bolt pattern in an assembly. When I click on each point and measure I get the x,y,z coordinates. I would like to be able to export these coordinates to a table format for subsequent in another program. Is this possible?

      At the moment I am writing down all data which is a nuisance.

      Any information appreciated.

      Regards
      Ed
      Design Engineer
        • Exporting x,y,z corrdinates
          Mahir Abrahim
          Look into using a Hole Table in a drawing. I don't use them, but I believe they will give you XY coordinates which you can export to a spreadsheet. Getting the Z coordinate may be not possible.
          • Exporting x,y,z corrdinates
            Tom Nicholson
            I'd say it would be possible with a macro..

            How to do it, I can't help with..
              • Exporting x,y,z corrdinates
                Ed Morris
                The 'points' I am referring to are in the assembly. By Bolt pattern I mean I select the hole cylindrical surface and insert a 'point'. From this I select measure to obtain the x,y,z coordinates.
                Yes, the coordinates are with reference to the assembly coordinate system.

                I could not find a way to export the coordinates of each point to a file that I can manipulate. I need to use these in another program.

                Any hints

                Ed
              • Exporting x,y,z corrdinates
                Josh Brady
                What kind of points? Are they points in a sketch? Where is the sketch located? In the bolt itself, in a hole wizard pattern on the component the bolts are fixing? In a sketch in the assembly? You want X, Y, and Z with respect to the main assembly's coordinate system?
                • Exporting x,y,z corrdinates
                  Josh Brady
                  Sorry, I'm still not sure exactly what you're doing. There are a lot of ways to "insert a point" in SolidWorks. Are the 'points' you are inserting part of a sketch, or are they reference geometry points (as inserted by Insert->Reference Geometry->Point...? If they are part of a sketch, is it a 2-d sketch or a 3-d sketch?
                  • Exporting x,y,z corrdinates
                    Josh Brady
                    In that case, this oughtta do it for you. It will output the coordinates of every reference point in the active document to an Excel file. If you need more decimal places, add zeroes into the "FMAT" constant at the top. The "SF" constant is a scale factor. The 1000 value is to convert meters (the underlying unit that SW actually works in) to millimeters. If you're looking for inches, use 1000/25.4.

                    Option Explicit

                    Const FMAT As String = "0.00"
                    Const SF As Double = 1000

                    Dim swApp As SldWorks.SldWorks
                    Dim Part As SldWorks.ModelDoc2
                    Dim Feature As SldWorks.Feature
                    Dim myRefPoint As SldWorks.RefPoint
                    Dim myMathPt As SldWorks.MathPoint
                    Dim ptData As Variant
                    Dim xlApp As Excel.Application
                    Dim xlBook As Excel.Workbook
                    Dim xlSheet As Excel.Worksheet
                    Const FirstRow As Long = 4
                    Const FirstCol As Long = 2
                    Dim CurRow As Long
                    Dim IDCol As Long
                    Dim Xcol As Long
                    Dim Ycol As Long
                    Dim Zcol As Long
                    Dim PtID As Variant
                    Dim i As Long

                    Sub main()

                    Set swApp = Application.SldWorks
                    Set Part = swApp.ActiveDoc

                    Set xlApp = CreateObject("Excel.Application")
                    xlApp.Visible = True
                    Set xlBook = xlApp.Workbooks.Add
                    Set xlSheet = xlBook.Worksheets("Sheet1")

                    CurRow = FirstRow
                    IDCol = FirstCol
                    Xcol = FirstCol + 1
                    Ycol = FirstCol + 2
                    Zcol = FirstCol + 3
                    xlSheet.Cells(CurRow, IDCol).Value = "'Point ID"
                    xlSheet.Cells(CurRow, Xcol).Value = "'X Coord"
                    xlSheet.Cells(CurRow, Ycol).Value = "'Y Coord"
                    xlSheet.Cells(CurRow, Zcol).Value = "'Z Coord"
                    CurRow = CurRow + 1

                    Set Feature = Part.FirstFeature
                    While Not Feature Is Nothing
                    If Feature.GetTypeName2 = "RefPoint" Then
                    Set myRefPoint = Feature.GetSpecificFeature2
                    Set myMathPt = myRefPoint.GetRefPoint
                    ptData = myMathPt.ArrayData
                    xlSheet.Cells(CurRow, IDCol).Value = Feature.Name
                    xlSheet.Cells(CurRow, Xcol).Value = Format(ptData(0) * SF, FMAT)
                    xlSheet.Cells(CurRow, Ycol).Value = Format(ptData(1) * SF, FMAT)
                    xlSheet.Cells(CurRow, Zcol).Value = Format(ptData(2) * SF, FMAT)
                    CurRow = CurRow + 1
                    End If
                    Set Feature = Feature.GetNextFeature
                    Wend

                    End Sub
                      • Exporting x,y,z corrdinates
                        Ed Morris
                        Dear Josh,

                        Thank you. That is exactly what I needed. I realise now how using macros may be powerful tool if one knows how to exploit them!

                        Is there a good source I could avail of for one to learn macros in Solidworks?

                        Cheers
                        Ed
                        • Re: Exporting x,y,z corrdinates
                          Steven Bird

                          Josh,

                           

                          Is it hard to tweak this macro code to look for points in a 3d sketch rather than ref points?  If you can do this that would be very helpful for me.  I would appreciate it very much.  I have tried to get this macro to run, just to see how it works, but have been unsuccessful.  How do I use this code? When I run it in SW it does nothing....there is no error.  I'm a newbie, so what's the most common newbie mistake?

                           

                          What would really be ideal for me and my company would be to be able to not only export the points, but to export the points in a user defined order.  In my wildest fantasies I we would be able to select a point, hit a button and have it copy the xyz's to a spreadsheet or text file.  Wow, that would be awesome. I don't know what it would take to do that?  Even if we can't pick the order, an export macro will help tremendously.

                           

                          Thanks for any help or insight that you can give.

                           

                          Cody

                            • Re: Exporting x,y,z corrdinates
                              Nick Grisco

                              Cody,

                               

                              Attached is a macro that I've been using for a while that exports 3Dsketch point to an excel file. Just select the 3Dsketch in your feature tree, run the macro and then format your table anyway you would like. The only thing I have run into with this macro is if any of the points have coincident relationships to other points, you will get double coordinates in your table. Hope that works for you!

                                • Re: Exporting x,y,z corrdinates
                                  Cody Munk

                                  Thanks Nick,

                                   

                                  It works great.  I have tried to modify the code to give me 8 decimal places.  If I change anything VB crashes when I try and run it.  Weird.  I can't figure it out.  Do you have any ideas?

                                   

                                  If Not sp Is Nothing And Not sheet Is Nothing And Not exApp Is Nothing Then
                                           sheet.Cells(3 + i, 1).Value = "Pt. " & i + 1
                                           sheet.Cells(3 + i, 2).Value = Round(sp.X * 1000 / 1, 8)
                                           sheet.Cells(3 + i, 3).Value = Round(sp.Y * 1000 / 1, 8)
                                           sheet.Cells(3 + i, 4).Value = Round(sp.Z * 1000 / 1, 8)
                                           exApp.Columns.AutoFit

                                   

                                          If Not sp Is Nothing And Not sheet Is Nothing And Not exApp Is Nothing Then
                                           sheet.Cells(3 + i, 1).Value = "Pt. " & i + 1
                                           sheet.Cells(3 + i, 2).Value = sp.X
                                           sheet.Cells(3 + i, 3).Value = Round(sp.Y * 1000 / 1, 3)
                                           sheet.Cells(3 + i, 4).Value = Round(sp.Z * 1000 / 1, 3)
                                           exApp.Columns.AutoFit

                                   

                                  Thanks,

                                  Cody

                                  SW 2009 sp2.1 x64 xp

                                  VB 6.5

                                  • Re: Exporting x,y,z corrdinates

                                    This marco works great but is there a way to only export the center point of arc in a sketch?

                                     

                                    Thanks,

                                    Chris

                                • Re: Exporting x,y,z corrdinates
                                  Mark Greenwell

                                  Hi Josh

                                   

                                  Is there any chance you could just post the macro and not the code.

                                   

                                  I have tried to copy and paste the code but could not get it to work. Don't know if i did this right.

                                   

                                  Thanks

                                   

                                  Mark (Solid|Works 2009)

                                  • Re: Exporting x,y,z corrdinates

                                    Hi Josh, I also tried it but the macro stops at the first 'dim' line with a 'user defined type not defined' error.

                                    My doubt is that "sldworks.sldworks" is not really a 'type'

                                    Do you know of the problem?

                                     

                                    Giotto

                                    • Re: Exporting x,y,z corrdinates
                                      Sammy Redshaw

                                      Can this loop through 3d sketches a export the XYZ data points and the output the sketch name in the excel file.

                                       

                                      sketch tree.PNG

                                        • Re: Exporting x,y,z corrdinates
                                          Sammy Redshaw

                                          done it

                                           

                                          Option Explicit

                                          Dim Feature As SldWorks.Feature
                                          Dim swApp As SldWorks.SldWorks
                                          Dim swModel As ModelDoc2
                                          Dim swFeat As SldWorks.Feature
                                          Dim swSubFeat As SldWorks.Feature
                                          Dim profileFeature As Sketch
                                          Dim sketchpoints As Variant
                                          Dim sketchPoint As SldWorks.sketchPoint
                                          Dim i As Integer
                                          Dim xlApp As Excel.Application
                                          Dim xlBook As Excel.Workbook
                                          Dim xlSheet As Excel.Worksheet
                                          Const FirstRow As Long = 4
                                          Const namerow As Long = 3
                                          Dim FirstCol As Long
                                          Dim CurRow As Long
                                          Dim IDCol As Long
                                          Dim Xcol As Long
                                          Dim Ycol As Long
                                          Dim Zcol As Long
                                          Dim v As Long


                                          'To run this macro open the model and run macro
                                          'You have been warned this with travese through allsketches

                                           

                                          Sub main()

                                          'open and format the excel sheet
                                          Set xlApp = CreateObject("Excel.Application")
                                          xlApp.Visible = True
                                          Set xlBook = xlApp.Workbooks.Add
                                          Set xlSheet = xlBook.Worksheets("Sheet1")

                                          CurRow = FirstRow
                                          FirstCol = 2
                                          IDCol = FirstCol
                                          Xcol = FirstCol + 1
                                          Ycol = FirstCol + 2
                                          Zcol = FirstCol + 3
                                          xlSheet.Cells(2, IDCol).Value = "All values are in mm"
                                          xlSheet.Cells(CurRow, IDCol).Value = "'Point ID"
                                          xlSheet.Cells(CurRow, Xcol).Value = "'X Coord"
                                          xlSheet.Cells(CurRow, Ycol).Value = "'Y Coord"
                                          xlSheet.Cells(CurRow, Zcol).Value = "'Z Coord"
                                          CurRow = CurRow + 1


                                          Set swApp = Application.SldWorks
                                          Set swModel = swApp.ActiveDoc
                                          Set swFeat = swModel.FirstFeature


                                          Do While Not swFeat Is Nothing
                                           
                                            Set swSubFeat = swFeat.GetFirstSubFeature
                                           
                                            ' Process feature
                                            DoFeature swFeat
                                           
                                            Do While Not swSubFeat Is Nothing


                                              ' Process feature
                                              DoFeature swSubFeat
                                             
                                              Set swSubFeat = swSubFeat.GetNextSubFeature


                                            Loop
                                            Set swFeat = swFeat.GetNextFeature
                                           
                                          Loop


                                          End Sub

                                           

                                          Private Sub DoFeature(feat As Feature)
                                             
                                              If feat.GetTypeName2 = "3DProfileFeature" Then
                                                  ' Do the business on the feature
                                                  Set profileFeature = feat.GetSpecificFeature2()
                                                 
                                               
                                              
                                               If CurRow > 5 Then
                                               CurRow = FirstRow
                                          FirstCol = FirstCol + 5
                                          IDCol = FirstCol
                                          Xcol = FirstCol + 1
                                          Ycol = FirstCol + 2
                                          Zcol = FirstCol + 3
                                          xlSheet.Cells(2, IDCol).Value = "All values are in mm"
                                          xlSheet.Cells(CurRow, IDCol).Value = "'Point ID"
                                          xlSheet.Cells(CurRow, Xcol).Value = "'X Coord"
                                          xlSheet.Cells(CurRow, Ycol).Value = "'Y Coord"
                                          xlSheet.Cells(CurRow, Zcol).Value = "'Z Coord"
                                            ' Get the name
                                          xlSheet.Cells(namerow, IDCol).Value = "Sketch Name: " & feat.Name
                                          CurRow = CurRow + 1
                                          v = 0
                                          Else
                                             ' Get the namae
                                          xlSheet.Cells((namerow), IDCol).Value = "Sketch Name: " & feat.Name
                                          End If

                                           

                                                 
                                                  ' Get the sketch segments
                                                  sketchpoints = profileFeature.GetSketchPoints2
                                                 

                                                  ' Loop all segments
                                                  For i = 0 To UBound(sketchpoints)
                                                     
                                                  Set sketchPoint = sketchpoints(i)
                                                 'Export points in mm to Excel
                                                      xlSheet.Cells(CurRow, IDCol).Value = ".Pt" & v + 1
                                                      xlSheet.Cells(CurRow, Xcol).Value = Format(sketchPoint.X * 1000)
                                                      xlSheet.Cells(CurRow, Ycol).Value = Format(sketchPoint.Y * 1000)
                                                      xlSheet.Cells(CurRow, Zcol).Value = Format(sketchPoint.Z * 1000)
                                                      CurRow = CurRow + 1
                                          v = v + 1
                                                  Next
                                                 
                                              End If


                                          End Sub

                                           

                                          Needs the code cleaning up a bit and some dialog boxes putting in. only works for 3d sketched not sure how to do it for 2d sketches as well which be xz or zy or xy?

                                          can you help?

                                      • Exporting x,y,z corrdinates
                                        Josh Brady
                                        If you are already experienced with VB or VBA then Luke Malpass has a book, and the API help is pretty decent once you figure out how the object structure works. If you can get hold of the API help for 2008 it's much easier to read than 2009's. If you don't have previous programming experience, I would advise getting started writing macros in Excel. There are lots of VBA for Excel tutorials available on the web.
                                        • Re: Exporting x,y,z corrdinates
                                          PRAKASH BABU

                                          Hi All,

                                           

                                          In SolidWorks We have an option for exporting Points which in turn create curves and points as output. Go to Add-ins and switch ON Scan to 3D option. Then click Curve Wizard Feature and under creation method use discrete and under Mesh/Cloud/File select Browse and under files of type choose Text files (.txt, .csv) and choose appropriate file and apply. You can able to see a curve with points generated. Then create a line thru the points and hide the curve. Hope this amy help you. Find the attached image for your reference.

                                           

                                          Regards,

                                          Prakash.

                                            • Re: Exporting x,y,z corrdinates
                                              Yasaman Khodadadegan

                                              Hi Prakash,

                                               

                                              I tried what you mentioned to export points and I exactly followed your instruction in 3D Scan, but it does not save any point in .txt file. If you have tried it, may you give me more details. I created a blank .txt file and browse it when choosing text files since it requires selecting a file as well.

                                               

                                              Thanks,

                                              Yasaman