Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations MintJulep on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

The sketch file macro 3

Status
Not open for further replies.

chemipoo

Computer
Apr 5, 2004
10
Hey guys,

I use MS Excel to keep my crucial points of a 3d sketch in spreadsheet format. To get the points info I have to click on each point and then copy paste it in excel and I feel really annoyed about doing that(since there's a lot of points). Is there any macro out there that can help store all the points coordinated of a 3d sketch in an Excel file?
 
Replies continue below

Recommended for you

Yes, it can be done. I did a quick API help search and came across 'Get Sketch Points Example (VB)'. This is a good starting point if you know a little about visual basic. I have some experience with macros and storing their values in excel so if you need any help let me know.

Brian
 
Depends on what you need the points in 3D sketches for. Generally with solid modellers there is no need to reply on point entities as was often done in the past. But I assume you know that.

3D sketches and point entities in general are a bit lacking still. You could add dimensions from the points to the standard planes and do a design table (maybe an auto-create) then the dims would reflect the points' coordinates and would update. If you use an external DT instead of an embedded one you could then link the values to you spreadsheet and it would all update automatically. That would avoid macros, API's etc.

John Richards Sr. Mech. Engr.
Rockwell Collins Flight Dynamics

There are only 10 types of people in the world - those who understand binary and those who don't.
 
Thank you for replying my post.

I tried your way first JNR however there's really a lot of points in the 3d sketch and I need a dimension for each coordinate (x, y and z) so 3 times the total number of points is the number of the dimensions. Also I don't know where the auto design table generator is, one that put each dim variable in a spread sheet so I try to look into that first, if you figure it out please let me know how.

Creigbm, you suggested an approach that certainly works at the end, but I have not touched VB yet (I am experienced in C++ and Java) so if I can't figure out any easier method-especially the one mentioned by JNR- I'll have to learn bunch of API functions for both excel and solid works. If you have any other tips I’ll be grateful if you post them.

I really appreciate your help guys.
 
I don't know where I got this macro or if it even works, but here you go.

Ken

Sketch Points to Data File Macro
Instructions:
Select the sketch that you wish to "read" and run the macro.
The macro will bring up excel and start filling the sheet with xyz point data.

Sub main()
Dim swApp As SldWorks.SldWorks
Dim doc As SldWorks.ModelDoc2
Dim part As SldWorks.PartDoc
Dim sm As SldWorks.SelectionMgr
Dim feat As SldWorks.feature
Dim sketch As SldWorks.sketch
Dim v As Variant
Dim i As Long
Dim sseg As SldWorks.SketchSegment
Dim sline As SldWorks.SketchLine
Dim sp As SldWorks.SketchPoint
Dim ep As SldWorks.SketchPoint
Dim s As String

Dim exApp As Excel.Application
Dim sheet As Excel.Worksheet

Set exApp = New Excel.Application
If Not exApp Is Nothing Then
exApp.Visible = True
If Not exApp Is Nothing Then
exApp.Workbooks.Add
Set sheet = exApp.ActiveSheet
If Not sheet Is Nothing Then
sheet.Cells(1, 2).Value = "X"
sheet.Cells(1, 3).Value = "Y"
sheet.Cells(1, 4).Value = "Z"
End If
End If
End If

Set swApp = GetObject(, "sldworks.application")
If Not swApp Is Nothing Then
Set doc = swApp.ActiveDoc
If Not doc Is Nothing Then
If doc.GetType = swDocPART Then
Set part = doc
Set sm = doc.SelectionManager
If Not part Is Nothing And Not sm Is Nothing Then
If sm.GetSelectedObjectType2(1) = swSelSKETCHES Then
Set feat = sm.GetSelectedObject4(1)
Set sketch = feat.GetSpecificFeature
If Not sketch Is Nothing Then
v = sketch.GetSketchPoints
For i = LBound(v) To UBound(v)
Set sp = v(i)
If Not sp Is Nothing And Not sheet Is Nothing And Not exApp Is Nothing Then
'sheet.Cells(2 + i, 1).Value = "Normal Vector " & i + 1
sheet.Cells(2 + i, 2).Value = Round(sp.x * 1000 / 25.4, DEC)
sheet.Cells(2 + i, 3).Value = Round(sp.y * 1000 / 25.4, DEC)
sheet.Cells(2 + i, 4).Value = Round(sp.z * 1000 / 25.4, DEC)
exApp.Columns.AutoFit
End If
Next i
End If
End If
End If
End If
End If
End If
End Sub

 
Thank you for your post KenBolen.The code seems reasonable and logical eonough but VB doesn't recognize most of the SW constants used. so I thought I should include a constat module file from ./samples/appcomm/swconst.bas with the macro project but still doesn't seem to work. I'll try to figure this out.
Thanks alot for the help.

Pooyan


 
This is was Steve told me from API support.
Hi Brad,

We no longer support swconst.bas. Instead, please use swconst.tlb, which you can add to your C++, VB6, or VB.NET project as a type library reference. swconst.tlb is in the same location as sldworks.tlb and sldworks.exe


Bradley
 
Thanks Bradley.
I guess you guys are using a newer version of SW because mine is 2002-2003 version and I couldn't find the swconst.tlb. In case I’m wrong and the file is somewhere on hard-drive, can you tell me what do you mean by adding it as a type library reference? Is there a especial method to do this or is it just accomplished by importing the tbl to the project? Please explain.
Thank you.
Pooyan
 
i get this errors alot :

User defined type is not defined.
Object variable or with block variable not set

I tried the following code from the sample help:

Sub GetSketchPoints()



Dim swApp As Object

Dim Part As Object

so how come I get the errors?


Dim sketchPointArray As Variant



' Optional (see below)

Dim theSketchPoint As Object

Dim pointCount As Integer

Dim xValue As Double

Dim yValue As Double

Dim zValue As Double



Set swApp = CreateObject("SldWorks.Application")

Set Part = swApp.ActiveDoc

Set theSketch = Part.GetActiveSketch2



sketchPointArray = theSketch.GetSketchPoints

pointCount = UBound(sketchPointArray) + 1



' For each SketchPoint

For i = 0 To (pointCount - 1)



' Set local SketchPoint object (optional)

' Set theSketchPoint = sketchPointArray(i)



' Get the coordinates

xValue = sketchPointArray(i).X

yValue = sketchPointArray(i).Y

zValue = sketchPointArray(i).Z



' <Do something useful with the data>



Next i



End Sub

 
Pooyan,
The swconst.tlb is linked in Visual Basic 6:
In main menu Project, References..., SolidWorks Constant type library.

Bradley
 
Sorry for reviving an old thread, but just in case if you need a swconst.bas for SolidWorks 2004 you can download this from
Go to the macro page and scroll to the end of the page, there is a "macro" called mm_998.zip.

I made this for my own needs to copy/paste just the few constants I need, but maybe you find it also useful.

HTH,
Stefan

--
unofficial german SolidWorks helppage
Shareware, freeware, tools and macros
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor