Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 6

Const CUT_LIST_PRPS_TRANSFER As Long =

swCutListTransferOptions_e.swCutListTransferOptions_FileProperties
Const OUT_DIR As String = ""

Option Explicit
Dim swApp As Object
Dim modelName As String
Dim MainPart As Object
Dim swPart As SldWorks.PartDoc
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Sub main()
Set swApp = Application.SldWorks

Set swPart = swApp.ActiveDoc


Set MainPart = swApp.ActiveDoc

modelName = Left(Mid(MainPart.GetPathName, InStrRev(MainPart.GetPathName, "\")


+ 1), InStrRev(Mid(MainPart.GetPathName, InStrRev(MainPart.GetPathName, "\") + 1),
".") - 1)

try_:
On Error GoTo catch_

Dim swModel As SldWorks.ModelDoc2

Set swModel = swApp.ActiveDoc

If Not swModel Is Nothing Then

Dim vCutLists As Variant


vCutLists = GetCutLists(swModel)

Dim i As Integer

For i = 0 To UBound(vCutLists)

Dim swCutListFeat As SldWorks.Feature


Set swCutListFeat = vCutLists(i)
Dim featName As String
featName = modelName & "-" & i + 1
If featName <> "" Then
If swCutListFeat.name <> featName Then
swCutListFeat.name = featName
End If
Else
Debug.Print "Empty name for " & swCutListFeat.name
End If

ProcessCutLists swCutListFeat
Next

Else
MsgBox "Please open the document"
End If

GoTo finally_
catch_:
swApp.SendMsgToUser2 Err.Description, swMessageBoxIcon_e.swMbStop,
swMessageBoxBtn_e.swMbOk
finally_:

Dim vBodies As Variant


vBodies = GetSelectedBodies(swPart.SelectionManager)

If IsEmpty(vBodies) Then
vBodies = swPart.GetBodies2(swBodyType_e.swSolidBody, True)
End If

Dim j As Integer

For j = 0 To UBound(vBodies)

Dim swBody As SldWorks.Body2


Set swBody = vBodies(j)

If False <> swBody.Select2(False, Nothing) Then

Dim outFilePath As String


outFilePath = GetOutFilePath(swPart, swBody, OUT_DIR)

Dim errs As Long


Dim warns As Long

If False <> swPart.SaveToFile3(outFilePath,


swSaveAsOptions_e.swSaveAsOptions_Silent, CUT_LIST_PRPS_TRANSFER, False, "", errs,
warns) Then
swApp.CloseDoc outFilePath
Else
Err.Raise vbError, "", "Failed to save body " & swBody.name & " to
file " & outFilePath & ". Error code: " & errs
End If

Else
Err.Raise vbError, "", "Failed to select body " & swBody.name
End If
Next

End Sub

Function GetSelectedBodies(selMgr As SldWorks.SelectionMgr) As Variant

Dim isInit As Boolean


isInit = False

Dim swBodies() As SldWorks.Body2

Dim i As Integer

For i = 1 To selMgr.GetSelectedObjectCount2(-1)

Dim swBody As SldWorks.Body2

Set swBody = GetSelectedObjectBody(selMgr, i)


If Not swBody Is Nothing Then

If Not isInit Then


ReDim swBodies(0)
Set swBodies(0) = swBody
isInit = True
Else
If Not Contains(swBodies, swBody) Then
ReDim Preserve swBodies(UBound(swBodies) + 1)
Set swBodies(UBound(swBodies)) = swBody
End If
End If

End If

Next

If isInit Then
GetSelectedBodies = swBodies
Else
GetSelectedBodies = Empty
End If

End Function

Function GetSelectedObjectBody(selMgr As SldWorks.SelectionMgr, index As Integer)


As SldWorks.Body2

Dim swBody As SldWorks.Body2

Dim selObj As Object


Set selObj = selMgr.GetSelectedObject6(index, -1)

If Not selObj Is Nothing Then


If TypeOf selObj Is SldWorks.Body2 Then
Set swBody = selObj
ElseIf TypeOf selObj Is SldWorks.Face2 Then
Dim swFace As SldWorks.Face2
Set swFace = selObj
Set swBody = swFace.GetBody
ElseIf TypeOf selObj Is SldWorks.Edge Then
Dim swEdge As SldWorks.Edge
Set swEdge = selObj
Set swBody = swEdge.GetBody
ElseIf TypeOf selObj Is SldWorks.Vertex Then
Dim swVertex As SldWorks.Vertex
Set swVertex = selObj
Set swBody = swVertex.GetBody
End If
End If

Set GetSelectedObjectBody = swBody

End Function

Function Contains(vArr As Variant, item As Object) As Boolean

Dim i As Integer
For i = 0 To UBound(vArr)
If vArr(i) Is item Then
Contains = True
Exit Function
End If
Next

Contains = False

End Function

Function GetOutFilePath(model As SldWorks.ModelDoc2, body As SldWorks.Body2, outDir


As String) As String

If outDir = "" Then


outDir = model.GetPathName()
If outDir = "" Then
Err.Raise vbError, "", "Output directory cannot be composed as file was
never saved"
End If

outDir = Left(outDir, InStrRev(outDir, "\") - 1)


End If

If Right(outDir, 1) = "\" Then


outDir = Left(outDir, Len(outDir) - 1)
End If

GetOutFilePath = ReplaceInvalidPathSymbols(outDir & "\" & body.name &


".sldprt")

End Function

Function ReplaceInvalidPathSymbols(path As String) As String

Const REPLACE_SYMB As String = "_"

Dim res As String


res = Right(path, Len(path) - Len("X:\"))

Dim drive As String


drive = Left(path, Len("X:\"))

Dim invalidSymbols As Variant


invalidSymbols = Array("/", ":", "*", "?", """", "<", ">", "|")

Dim i As Integer
For i = 0 To UBound(invalidSymbols)
Dim invalidSymb As String
invalidSymb = CStr(invalidSymbols(i))
res = Replace(res, invalidSymb, REPLACE_SYMB)
Next

ReplaceInvalidPathSymbols = drive + res

End Function

Function GetCutLists(model As SldWorks.ModelDoc2) As Variant


GetCutLists = GetFeaturesByType(model, "CutListFolder")

End Function

Function GetFeaturesByType(model As SldWorks.ModelDoc2, typeName As String) As


Variant

Dim swFeats() As SldWorks.Feature

Dim swFeat As SldWorks.Feature

Set swFeat = model.FirstFeature

Do While Not swFeat Is Nothing

ProcessFeature swFeat, swFeats, typeName

Set swFeat = swFeat.GetNextFeature

Loop

If (Not swFeats) = -1 Then


GetFeaturesByType = Empty
Else
GetFeaturesByType = swFeats
End If

End Function

Sub ProcessFeature(thisFeat As SldWorks.Feature, featsArr() As SldWorks.Feature,


typeName As String)

If thisFeat.GetTypeName2() = typeName Then

If (Not featsArr) = -1 Then


ReDim featsArr(0)
Set featsArr(0) = thisFeat
Else
Dim i As Integer

For i = 0 To UBound(featsArr)
If swApp.IsSame(featsArr(i), thisFeat) =
swObjectEquality.swObjectSame Then
Exit Sub
End If
Next

ReDim Preserve featsArr(UBound(featsArr) + 1)


Set featsArr(UBound(featsArr)) = thisFeat
End If

End If

Dim swSubFeat As SldWorks.Feature


Set swSubFeat = thisFeat.GetFirstSubFeature

While Not swSubFeat Is Nothing


ProcessFeature swSubFeat, featsArr, typeName
Set swSubFeat = swSubFeat.GetNextSubFeature
Wend

End Sub

Function ProcessCutLists(swFeat As SldWorks.Feature)

Dim swBodyFolder As SldWorks.BodyFolder

If swFeat.GetTypeName2() = "SolidBodyFolder" Then


Set swBodyFolder = swFeat.GetSpecificFeature2
swBodyFolder.UpdateCutList
ElseIf swFeat.GetTypeName2() = "CutListFolder" Then
Set swBodyFolder = swFeat.GetSpecificFeature2

Dim name As String


name = swFeat.name

RenameBodies swBodyFolder.GetBodies(), name

End If

Set swFeat = swFeat.GetNextFeature

End Function

Function RenameBodies(bodies As Variant, bodyName As String)


Dim myModelView As Object
Set myModelView = swPart.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized

If Not IsEmpty(bodies) Then

Dim i As Integer
Dim check As Integer
check = 1
For i = 0 To UBound(bodies)
Dim swBody As SldWorks.Body2
Set swBody = bodies(i)
swBody.name = bodyName & "-" & i + 1
Do While check = 1
boolstatus = swPart.Extension.SelectByID2(bodyName & "-" & i + 1,
"SOLIDBODY", 0, 0, 0, True, 0, Nothing, 0)
swBody.name = bodyName
check = 0
Loop
Next

End If

End Function

You might also like