This website uses cookies to ensure you get the best experience on our website. By using our website you agree on the following Cookie Policy, Privacy Policy, and Terms Of Use
Const SUFFIX AsString = "_FP"Dim swApp As SldWorks.SldWorks
Sub main()
Set swApp = Application.SldWorks
Dim swModel As SldWorks.ModelDoc2
Set swModel = swApp.ActiveDoc
try_:
OnErrorGoTo catch_
Dim vCutListFeats AsVariant
vCutListFeats = GetCutListFeatures(swModel)
IfNot IsEmpty(vCutListFeats) ThenDim vFlatPatternFeats AsVariant
vFlatPatternFeats = GetFlatPatternFeatures(swModel)
IfNot IsEmpty(vFlatPatternFeats) Then
RenameFlatPatternsWithCutList swModel, vFlatPatternFeats, vCutListFeats
Else
Err.Raise vbError, "", "No flat pattern features found"EndIfElse
Err.Raise vbError, "", "No cut-list items found"EndIfGoTo finally_
catch_:
swApp.SendMsgToUser2 Err.Description, swMessageBoxIcon_e.swMbStop, swMessageBoxBtn_e.swMbOk
finally_:
EndSubFunction GetCutListFeatures(model As SldWorks.ModelDoc2) AsVariant
GetCutListFeatures = GetFeaturesByType(model, "CutListFolder")
EndFunctionFunction GetFlatPatternFeatures(model As SldWorks.ModelDoc2) AsVariant
GetFlatPatternFeatures = GetFeaturesByType(model, "FlatPattern")
EndFunctionFunction RenameFlatPatternsWithCutList(model As SldWorks.ModelDoc2, vFlatPatternFeats AsVariant, vCutListFeats AsVariant)
Dim i AsIntegerFor i = 0 To UBound(vFlatPatternFeats)
Dim swFlatPatternFeat As SldWorks.Feature
Dim swFlatPattern As SldWorks.FlatPatternFeatureData
Set swFlatPatternFeat = vFlatPatternFeats(i)
Set swFlatPattern = swFlatPatternFeat.GetDefinition
Dim swFixedFace As SldWorks.Face2
Set swFixedFace = swFlatPattern.FixedFace2
Dim swBody As SldWorks.Body2
Set swBody = swFixedFace.GetBody
Dim swCutListFeat As SldWorks.Feature
Set swCutListFeat = FindCutListFeature(vCutListFeats, swBody)
IfNot swCutListFeat IsNothingThenIf swFlatPatternFeat.Name <> swCutListFeat.Name ThenDim featName AsString
featName = swCutListFeat.Name + SUFFIX
Dim index AsInteger
index = 0
While model.FeatureManager.IsNameUsed(swNameType_e.swFeatureName, featName)
index = index + 1
featName = swCutListFeat.Name + CStr(index) + SUFFIX
Wend
swFlatPatternFeat.Name = featName
EndIfEndIfNextEndFunctionFunction FindCutListFeature(vCutListFeats AsVariant, body As SldWorks.Body2) As SldWorks.Feature
Dim i AsIntegerFor i = 0 To UBound(vCutListFeats)
Dim swCutListFeat As SldWorks.Feature
Set swCutListFeat = vCutListFeats(i)
Dim swBodyFolder As SldWorks.BodyFolder
Set swBodyFolder = swCutListFeat.GetSpecificFeature2
Dim vBodies AsVariant
vBodies = swBodyFolder.GetBodies
If ContainsBody(vBodies, body) ThenSet FindCutListFeature = swCutListFeat
EndIfNextEndFunctionFunction ContainsBody(vBodies AsVariant, body As SldWorks.Body2) AsBooleanIfNot IsEmpty(vBodies) ThenDim i AsIntegerFor i = 0 To UBound(vBodies)
Dim swCutListBody As SldWorks.Body2
Set swCutListBody = vBodies(i)
If swApp.IsSame(swCutListBody, body) = swObjectEquality.swObjectSame Then
ContainsBody = TrueExitFunctionEndIfNextEndIf
ContainsBody = FalseEndFunctionFunction GetFeaturesByType(model As SldWorks.ModelDoc2, typeName AsString) AsVariantDim swFeats() As SldWorks.Feature
Dim swFeat As SldWorks.Feature
Set swFeat = model.FirstFeature
DoWhileNot swFeat IsNothing
ProcessFeature swFeat, swFeats, typeName
Set swFeat = swFeat.GetNextFeature
LoopIf (Not swFeats) = -1 Then
GetFeaturesByType = Empty
Else
GetFeaturesByType = swFeats
EndIfEndFunctionSub ProcessFeature(thisFeat As SldWorks.Feature, featsArr() As SldWorks.Feature, typeName AsString)
If thisFeat.GetTypeName2() = typeName ThenIf (Not featsArr) = -1 ThenReDim featsArr(0)
Set featsArr(0) = thisFeat
ElseDim i AsIntegerFor i = 0 To UBound(featsArr)
If swApp.IsSame(featsArr(i), thisFeat) = swObjectEquality.swObjectSame ThenExitSubEndIfNextReDimPreserve featsArr(UBound(featsArr) + 1)
Set featsArr(UBound(featsArr)) = thisFeat
EndIfEndIfDim swSubFeat As SldWorks.Feature
Set swSubFeat = thisFeat.GetFirstSubFeature
WhileNot swSubFeat IsNothing
ProcessFeature swSubFeat, featsArr, typeName
Set swSubFeat = swSubFeat.GetNextSubFeature
Wend
EndSub
Notifications
Join session by SOLIDWORKS and PDM API expret Artem Taturevych at 3DEXPERIENCE World 2025 on Feb 26 at 08:30 AM CST to explore 10 essential macros for automating drawings, assemblies, custom properties, and more