Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

SolidWorks API, how can I know wich parameters are equation driven?

Status
Not open for further replies.

ManuOrb

Mechanical
Jun 22, 2009
3
0
0
ES
Hi, I've write (taking code from here and there in Internet) a script to extract all dimensions from some drawings so I can work with them with Excel. The thing is some of these are formula driven in the drawing so I don't want to extract those. Any ideas?

See the code I wrote.

Regards,
Manolo

'Dimensions in m and rad
'In d:\tmp only PART and ASSEMBLY files
Sub main()
'Variables SW
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swFeat As SldWorks.Feature
Dim swSubFeat As SldWorks.Feature
Dim swDispDim As SldWorks.DisplayDimension
Dim swDim As SldWorks.Dimension
Dim swAnn As SldWorks.Annotation
Dim bRet As Boolean
Dim sCadena As String
Dim sFicheroTemp As String
Dim sFichero As String

'Variables for files
Dim fso As FileSystemObject
Dim objFile As File
Dim objFolder As Folder
Dim objFileCol As Variant
Dim FolderName As String
Dim FileName As String
Dim objRegEx As RegExp
Dim lErrors As Long
Dim lWarnings As Long

Set swApp = Application.SldWorks
Set fso = New FileSystemObject
Set objRegEx = New RegExp
objRegEx.IgnoreCase = True
objRegEx.Pattern = "\.SLDPRT"

FolderName = "D:\tmp\"
Set objFolder = fso.GetFolder(FolderName)
Set objFileCol = objFolder.Files
sFicheroTemp = FolderName & "temp.txt"
For Each objFile In objFileCol
FileName = FolderName + objFile.Name
If objRegEx.Test(objFile.Name) Then
Set swModel = swApp.OpenDoc6(FileName, swDocPART, swOpenDocOptions_ReadOnly, "", lErrors, lWarnings)
Else
Set swModel = swApp.OpenDoc6(FileName, swDocASSEMBLY, swOpenDocOptions_ReadOnly, "", lErrors, lWarnings)
End If

Set swFeat = swModel.FirstFeature
Do While Not swFeat Is Nothing
Set swDispDim = swFeat.GetFirstDisplayDimension
Do While Not swDispDim Is Nothing
Set swAnn = swDispDim.GetAnnotation
Set swDim = swDispDim.GetDimension
sCadena = swDim.FullName & "*" & swDim.GetSystemValue2("")
Open sFicheroTemp For Append Access Write As #1
Print #1, sCadena
Close #1
'Sigue con el Loop
Set swDispDim = swFeat.GetNextDisplayDimension(swDispDim)
Loop
Set swFeat = swFeat.GetNextFeature
Loop
bRet = swApp.CloseAllDocuments(False)
Next

'Eliminar las líneas repetidas

Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H1
Dim objConnection As Object
Dim objRecordset As Object
Dim strPathtoTextFile As String
Dim strFile As String


Set objConnection = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")

strPathtoTextFile = "D:\tmp\"
strFile = "temp.txt"
sFichero = FolderName & "variables.txt"

objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strPathtoTextFile & ";" & _
"Extended Properties=""text;HDR=NO;FMT=Delimited"""

objRecordset.Open "Select DISTINCT * FROM " & strFile, _
objConnection, adOpenStatic, adLockOptimistic, adCmdText

Open sFichero For Output Access Write As #1
Do Until objRecordset.EOF
sCadena = objRecordset.Fields.Item(0).Value
sCadena = Replace(sCadena, "*", ";")
Print #1, sCadena
'Wscript.Echo objRecordset.Fields.Item(0).Value
objRecordset.MoveNext
Loop
Close #1
End Sub
 
Replies continue below

Recommended for you

You should use the IDimension::DrivenState property. If the returned value is swDimensionDriven then the dimension driven by equation.

Please take a look at the following macro. If the selected dimension is driven the output value is True and False in another case:

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swDispDim As SldWorks.DisplayDimension
Dim swDim As SldWorks.Dimension

Sub main()

Set swApp = Application.SldWorks

Set swModel = swApp.ActiveDoc

Set swSelMgr = swModel.SelectionManager

Set swDispDim = swSelMgr.GetSelectedObject6(1, -1)

Set swDim = swDispDim.GetDimension2(1)

Debug.Print swDim.DrivenState = swDimensionDrivenState_e.swDimensionDriven

End Sub


Artem Taturevich
CSWP
 
Thanks very much. Here is the complete macro to extract al dimensions that are not equation driven from all PART or ASM files in D:\tmp (there can't be any other type of files).

Sub main()
'Variables SW
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swFeat As SldWorks.Feature
Dim swSubFeat As SldWorks.Feature
Dim swDispDim As SldWorks.DisplayDimension
Dim swDim As SldWorks.Dimension
Dim swAnn As SldWorks.Annotation
Dim bRet As Boolean
Dim sCadena As String
Dim sFicheroTemp As String
Dim sFichero As String

'Variables para ficheros
Dim fso As FileSystemObject
Dim objFile As File
Dim objFolder As Folder
Dim objFileCol As Variant
Dim FolderName As String
Dim FileName As String
Dim objRegEx As RegExp
Dim lErrors As Long
Dim lWarnings As Long

Set swApp = Application.SldWorks
Set fso = New FileSystemObject
Set objRegEx = New RegExp
objRegEx.IgnoreCase = True
objRegEx.Pattern = "\.SLDPRT"

FolderName = "D:\tmp\"
Set objFolder = fso.GetFolder(FolderName)
Set objFileCol = objFolder.Files
sFicheroTemp = FolderName & "temp.txt"
For Each objFile In objFileCol 'Para cada archivo en la carpeta
FileName = FolderName + objFile.Name
If objRegEx.Test(objFile.Name) Then
Set swModel = swApp.OpenDoc6(FileName, swDocPART, swOpenDocOptions_ReadOnly, "", lErrors, lWarnings)
Else
Set swModel = swApp.OpenDoc6(FileName, swDocASSEMBLY, swOpenDocOptions_ReadOnly, "", lErrors, lWarnings)
End If

'Busca todas las dimensiones del archivo
Set swFeat = swModel.FirstFeature
Do While Not swFeat Is Nothing
Set swDispDim = swFeat.GetFirstDisplayDimension
Do While Not swDispDim Is Nothing
Set swAnn = swDispDim.GetAnnotation
Set swDim = swDispDim.GetDimension

If swDim.DrivenState <> swDimensionDrivenState_e.swDimensionDriven Then
sCadena = swDim.FullName & "*" & swDim.GetSystemValue2("")
'Añade las variables al fichero de texto
Open sFicheroTemp For Append Access Write As #1
Print #1, sCadena
Close #1
'Sigue con el Loop
End If
Set swDispDim = swFeat.GetNextDisplayDimension(swDispDim)
Loop
Set swFeat = swFeat.GetNextFeature
Loop
bRet = swApp.CloseAllDocuments(False)
Next

'Eliminar las líneas repetidas

Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H1
Dim objConnection As Object
Dim objRecordset As Object
Dim strPathtoTextFile As String
Dim strFile As String


Set objConnection = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")

strPathtoTextFile = "D:\tmp\"
strFile = "temp.txt"
sFichero = FolderName & "variables.txt"

objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strPathtoTextFile & ";" & _
"Extended Properties=""text;HDR=NO;FMT=Delimited"""

objRecordset.Open "Select DISTINCT * FROM " & strFile, _
objConnection, adOpenStatic, adLockOptimistic, adCmdText

Open sFichero For Output Access Write As #1
Do Until objRecordset.EOF
sCadena = objRecordset.Fields.Item(0).Value
sCadena = Replace(sCadena, "*", ";")
Print #1, sCadena
'Wscript.Echo objRecordset.Fields.Item(0).Value
objRecordset.MoveNext
Loop
Close #1
End Sub
 
The most certain way to determine which parameters are equation driven is to access the Equation Mnaager. From there, go through each of the equations and parse out which parameters are being driven.

[bat]Honesty may be the best policy, but insanity is a better defense.[bat]
-SolidWorks API VB programming help
 
Status
Not open for further replies.
Back
Top