Hello Everyone!
I am in search of a macro that only exports out to excel dimensions + tolerances that are marked for inspection (ie the ones with the circle/pill capsule around them). I know Solidworks Makes an Inspection Tool/add-in that does this, but I only need this one feature.
I have found some macros on here, but they export out all of the dimensions including the one pertaining to the sheet.
Any help would be great!
Ryan
SolidworksApi macros
Recommend getting your feet wet editing code.
Find one that does as close as possible to what you want. Somewhere in that code will be something like:
set swDispDim=(something or other)
This is the display dimension object. You can test this object to see if it is marked for inspection with .Inspection.
So, somewhere after that line you would put:
If swDispDim.Inspection Then
[put all the code that does the exporting inside this "If" block.]
[put the "End If" right before it grabs the next dimension. Probably with something like "swDispDim.GetNext"]
End if
Great! I was able to modify some existing code that I found on the forums to get it to do what I wanted it to. My next question is how do I get it to cycle through all of the Sheets rather than the active sheet?
I also would like to pull the part name (6 digit number), description and Revision that are stored in the custom properties.
Here is my code that I modified... I got it from a search on the forums
Dim swApp As SldWorks.SldWorks
Dim swDoc As SldWorks.ModelDoc2
Dim swDwg As SldWorks.DrawingDoc
Dim swView As SldWorks.View
Dim swDispDim As SldWorks.DisplayDimension
Dim swDim As SldWorks.Dimension
Dim swTol As SldWorks.DimensionTolerance
Dim xlApp As Excel.Application
Dim xlSheet As Excel.Worksheet
Dim xlBooks As Excel.Workbooks
Dim CurRow As Long
Const STARTROW As Long = 1
Const PARTNAME As Long = 1
Const DIMNAMECOL As Long = 2
Const INSPECTION As Long = 3
Const DIMVALCOL As Long = 4
Const DIMTOLTYPECOL As Long = 5
Const DIMUPPERTOLVALCOL As Long = 6
Const DIMLOWERTOLVALCOL As Long = 7
Const DIMTOLFITCLASSCOL As Long = 8
Const TOLSCALEFACTOR As Double = 1000
Sub ExportDims()
Dim NumTolVals As Integer
Dim TolIsFit As Boolean
Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
If swDoc.GetType <> swDocDRAWING Then
MsgBox "This macro only works for drawing files."
Exit Sub
End If
Set swDwg = swDoc
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlBooks = xlApp.Workbooks
xlBooks.Add
Set xlSheet = xlApp.ActiveSheet
CurRow = STARTROW
xlSheet.Cells(CurRow, PARTNAME).Value = "Part Name"
xlSheet.Cells(CurRow, DIMNAMECOL).Value = "Dimension Name"
xlSheet.Cells(CurRow, INSPECTION).Value = "Inspection Dimension"
xlSheet.Cells(CurRow, DIMVALCOL).Value = "Nominal Value"
xlSheet.Cells(CurRow, DIMTOLTYPECOL).Value = "Tolerance Type"
xlSheet.Cells(CurRow, DIMUPPERTOLVALCOL).Value = "Upper Tol"
xlSheet.Cells(CurRow, DIMLOWERTOLVALCOL).Value = "Lower Tol"
CurRow = CurRow + 1
Set swView = swDwg.GetFirstView
While Not (swView Is Nothing)
Set swDispDim = swView.GetFirstDisplayDimension5
While Not swDispDim Is Nothing
Set swDim = swDispDim.GetDimension
Set swTol = swDim.Tolerance
If swDispDim.INSPECTION Then
xlSheet.Cells(CurRow - 6, INSPECTION).Value = swDispDim.INSPECTION
xlSheet.Cells(CurRow - 6, DIMNAMECOL).Value = swDim.FullName
xlSheet.Cells(CurRow - 6, DIMVALCOL).Value = swDim.Value
xlSheet.Cells(CurRow - 6, DIMTOLTYPECOL).Value = GetTolTypeName(swTol, NumTolVals, TolIsFit)
If NumTolVals > 0 Then
xlSheet.Cells(CurRow - 6, DIMUPPERTOLVALCOL).Value = swTol.GetMaxValue * TOLSCALEFACTOR
End If
If NumTolVals > 1 Then
xlSheet.Cells(CurRow - 6, DIMLOWERTOLVALCOL).Value = swTol.GetMinValue * TOLSCALEFACTOR
End If
End If
Set swDispDim = swDispDim.GetNext5
CurRow = CurRow + 1
Wend
Set swView = swView.GetNextView
Wend
xlApp.Range("A:Z").EntireColumn.AutoFit
End Sub
Function GetTolTypeName(ByRef myTol As SldWorks.DimensionTolerance, ByRef NumVals As Integer, ByRef FitTol As Boolean) As String
Dim s As String
FitTol = False
Select Case myTol.Type
Case swTolNONE
s = "None"
NumVals = 0
Case swTolInspection
s = "Inspection"
NumVals = 2
Case swTolBASIC
s = "Basic"
NumVals = 0
Case swTolBILAT
s = "Bilateral"
NumVals = 2
Case swTolLIMIT
s = "Limit"
NumVals = 2
Case swTolSYMMETRIC
s = "Symmetric"
NumVals = 1
Case swTolMIN
s = "Minimum"
NumVals = 0
Case swTolMAX
s = "Maximum"
NumVals = 0
Case swTolMETRIC
s = "Metric"
NumVals = 2
Case swTolFIT
s = "Fit"
NumVals = 0
FitTol = True
Case swTolFITWITHTOL
s = "Fit With Tolerance"
NumVals = 2
FitTol = True
Case swTolFITTOLONLY
s = "Fit, Tolerance Only"
NumVals = 2
FitTol = False
Case swTolBLOCK
s = "Block"
NumVals = 2
FitTol = False
End Select
GetTolTypeName = s
End Function
Thanks!
Ryan
You can read properties using the CustomPropertyManager interface.
To cycle through the sheets, I would recommend getting an array of the sheet names using GetSheetNames, then looping through that array, activating each sheet by name with ActivateSheet. This new loop would enclose the block of existing the code starting with Set SwView = swDwg.GetFirstView and ending with Wend
here is a code to do the following: Code to export inspection dimensions only (with oval frame) to excel.
'data included: full name, nominal -mm, nominal -in, production QC-mm, auto fill production QC-in, enter QC-in.
'code will name excel file as the part and will save in the drawing directory, code will name sheet as the part, code will input a blank row between drawing views Attribute VB_Name = "copydims1"
'============================
Dim swApp As Object
Dim swDoc As SldWorks.ModelDoc2
Dim swDwg As SldWorks.DrawingDoc
Dim swView As SldWorks.View
Dim swDispDim As SldWorks.DisplayDimension
Dim swDim As SldWorks.Dimension
Dim swTol As SldWorks.DimensionTolerance
Dim xlApp As Excel.Application
Dim xlSheet As Excel.Worksheet
Dim xlBooks As Excel.Workbooks
Dim CurRow As Long
Const STARTROW As Long = 1
Const DIMNAMECOL As Long = 1
Const DIMVALCOL As Long = 2
Const DIMVALCOL2 As Long = 3
Const DIMTOLTYPECOL As Long = 4
Const DIMTOLTYPECOL2 As Long = 5
Const DIMUPPERTOLVALCOL As Long = 6
Const DIMLOWERTOLVALCOL As Long = 8
Const DIMTOLFITCLASSCOL As Long = 8
Const TOLSCALEFACTOR As Double = 1000
'Code to export inspection dimensions only (with oval frame) to excel.
'data included: full name, nominal -mm, nominal -in, production QC-mm, auto fill production QC-in, enter QC-in.
'code will name excel file as the part, code will name sheet as the part, code will input a blank row between drawing views.
Sub copydims1()
Dim lastview As String
Dim mydim As String
Dim NumTolVals As Integer
Dim TolIsFit As Boolean
Dim swNote As Object
Dim outline() As Double
Dim pos() As Double
Dim boolstatus As Boolean
Dim MyView As String
Dim Part As SldWorks.ModelDoc2
Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
Set Part = swApp.ActiveDoc
If swDoc.GetType <> swDocDRAWING Then
MsgBox "This macro only works for drawing files."
Exit Sub
End If
Set swDwg = swDoc
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlBooks = xlApp.Workbooks
xlBooks.Add
Set xlSheet = xlApp.ActiveSheet
CurRow = STARTROW
xlSheet.Cells(CurRow, DIMNAMECOL).Value = "Dimension Name" '1
xlSheet.Cells(CurRow, DIMVALCOL).Value = "Nominal Value (mm)" '2
xlSheet.Cells(CurRow, DIMVALCOL2).Value = "Nominal Value (Inch)" '3
xlSheet.Cells(CurRow, DIMTOLTYPECOL).Value = "PODUCTION DIM VALUE (mm)" '4
xlSheet.Cells(CurRow, DIMTOLTYPECOL2).Value = "PODUCTION DIM VALUE (Inch)" '5
xlSheet.Cells(CurRow, DIMUPPERTOLVALCOL).Value = "INSPECTION DIM VALUE (Inch)" '6
CurRow = CurRow + 1
Set swView = swDwg.GetFirstView
While Not (swView Is Nothing)
Set swDispDim = swView.GetFirstDisplayDimension5
While Not swDispDim Is Nothing
Set swDim = swDispDim.GetDimension
mydim = xlSheet.Cells(CurRow - 1, DIMNAMECOL).Value 'swDim.FullName
lastview = Right(mydim, Len(mydim) - 4)
If swDispDim.Inspection Then
If Right(swDim.FullName, Len(swDim.FullName) - 4) <> lastview And CurRow > 2 Then
CurRow = CurRow + 1
End If
'Set swDim = swDispDim.GetDimension
Set swTol = swDim.Tolerance
xlSheet.Cells(CurRow, DIMNAMECOL).Value = swDim.FullName
xlSheet.Cells(CurRow, DIMVALCOL).Value = Round(swDim.Value, 1) * 25.4
xlSheet.Cells(CurRow, DIMVALCOL2).Value = Round(swDim.Value, 3)
xlSheet.Cells(CurRow, DIMTOLTYPECOL).Value = "Production Enter" 'Round(swDim.Value, 3) * 25.4 'GetTolTypeName(swTol, NumTolVals, TolIsFit)
xlSheet.Cells(CurRow, DIMTOLTYPECOL2).Value = "=IFERROR(ROUND(RC[-1]/25.4,3),""Auto fill"")" '"=Round(RC[-1]/25.4,3)" 'xlSheet.Cells(CurRow, DIMTOLTYPECOL).Value
xlSheet.Cells(CurRow, DIMUPPERTOLVALCOL).Value = "QC Enter"
CurRow = CurRow + 1
End If
If TolIsFit Then xlSheet.Cells(CurRow, DIMTOLFITCLASSCOL).Value = swTol.GetHoleFitValue & "/" & swTol.GetShaftFitValue
Set swDispDim = swDispDim.GetNext5
Wend
Set swView = swView.GetNextView
Wend
'===========================
Dim Shortname As String 'short
Dim longname As String 'long
Dim sheetname As String
Dim FilePath As String
Dim Lext As Integer
Dim Lsheetname As Integer
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Shortname = Part.GetTitle()
FilePath = Part.GetPathName()
'MsgBox "Shortname-" & Shortname
Lsheetname = InStr(5, Shortname, " -")
'MsgBox "Lsheetname-" & Lsheetname
If InStr(5, Shortname, " -") > 1 Then
Lsheetname = Len(Shortname) - InStr(5, Shortname, " -") + 3
'MsgBox "IF Lsheetname-" & Lsheetname
Lext = Len(FilePath) - 1 - InStr(3, FilePath, ".S")
longname = Left(FilePath, Len(FilePath) - 1 - Lext)
Shortname = Right(Shortname, Len(Shortname) - Lsheetname)
End If
'MsgBox "Shortname-" & Shortname
sheetname = Left(Part.GetTitle(), Len(Part.GetTitle()) - Len(Shortname))
'MsgBox "sheetname-" & sheetname
If InStr(3, FilePath, ".S") > 1 Then
Lext = Len(FilePath) - 1 - InStr(3, FilePath, ".S")
longname = Left(FilePath, Len(FilePath) - 1 - Lext)
End If
'MsgBox "FilePath-" & FilePath
'MsgBox "Lext-" & Lext
'MsgBox "Longname-" & longname
'MsgBox "Shortname-" & Shortname
xlApp.Range("A:Z").EntireColumn.AutoFit
xlApp.ActiveSheet.Name = sheetname
xlApp.Range("A1").Select
xlApp.ActiveWorkbook.SaveAs FileName:=Trim(longname) & "xlsx", _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
xlApp.ActiveWorkbook.Close
xlApp.Quit
Set xlApp = Nothing
MsgBox "Done with Dimensions for-" & longname
End Sub
Function GetTolTypeName(ByRef myTol As SldWorks.DimensionTolerance, ByRef NumVals As Integer, ByRef FitTol As Boolean) As String
Dim s As String
FitTol = False
Select Case myTol.Type
Case swTolNONE
s = "None"
NumVals = 0
Case swTolBASIC
s = "Basic"
NumVals = 0
Case swTolBILAT
s = "Bilateral"
NumVals = 2
Case swTolLIMIT
s = "Limit"
NumVals = 2
Case swTolSYMMETRIC
s = "Symmetric"
NumVals = 1
Case swTolMIN
s = "Minimum"
NumVals = 0
Case swTolMAX
s = "Maximum"
NumVals = 0
Case swTolMETRIC
s = "Metric"
NumVals = 2
Case swTolFIT
s = "Fit"
NumVals = 0
FitTol = True
Case swTolFITWITHTOL
s = "Fit With Tolerance"
NumVals = 2
FitTol = True
Case swTolFITTOLONLY
s = "Fit, Tolerance Only"
NumVals = 2
FitTol = False
Case swTolBLOCK
s = "Block"
NumVals = 2
FitTol = False
End Select
GetTolTypeName = s
End Function