Skip to content

Commit 11d447a

Browse files
author
Artem Taturevych
committed
Updated defeature part macro
1 parent b038f71 commit 11d447a

File tree

2 files changed

+83
-42
lines changed

2 files changed

+83
-42
lines changed

solidworks-api/document/features-manager/defeature-part/Macro.vba

Lines changed: 81 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,88 +4,129 @@ Sub main()
44

55
Set swApp = Application.SldWorks
66

7+
try_:
8+
9+
On Error GoTo catch_
10+
711
Dim swPart As SldWorks.PartDoc
812

913
Set swPart = swApp.ActiveDoc
1014

1115
If Not swPart Is Nothing Then
1216

13-
Dim vBodies As Variant
14-
vBodies = GetBodyCopies(swPart)
17+
Dim vUserFeats As Variant
18+
vUserFeats = GetAllTopLevelUserFeatures(swPart)
1519

16-
DeleteAllUserFeatures swPart
17-
18-
CreateFeaturesForBodies swPart, vBodies
20+
If Not IsEmpty(vUserFeats) Then
21+
CreateFeaturesForBodies swPart
22+
DeleteFeatures swPart, vUserFeats
23+
Else
24+
Err.Raise vbError, "", "No features in the model"
25+
End If
1926

2027
Else
2128
MsgBox "Please open part document"
2229
End If
2330

31+
GoTo finally_
32+
33+
catch_:
34+
MsgBox Err.Description, vbCritical
35+
finally_:
36+
2437
End Sub
2538

26-
Function GetBodyCopies(part As SldWorks.PartDoc) As Variant
39+
Sub CreateFeaturesForBodies(part As SldWorks.PartDoc)
2740

2841
Dim vBodies As Variant
29-
30-
vBodies = part.GetBodies2(swBodyType_e.swAllBodies, True)
31-
Dim i As Integer
3242

33-
For i = 0 To UBound(vBodies)
43+
vBodies = part.GetBodies2(swBodyType_e.swAllBodies, False)
44+
45+
If Not IsEmpty(vBodies) Then
46+
47+
Dim i As Integer
3448

35-
Dim swBody As SldWorks.Body2
36-
Set swBody = vBodies(i)
37-
Set swBody = swBody.Copy()
38-
Set vBodies(i) = swBody
49+
For i = 0 To UBound(vBodies)
50+
51+
Dim swBody As SldWorks.Body2
52+
Set swBody = vBodies(i)
53+
Set swBodyCopy = swBody.Copy()
54+
55+
Dim swFeat As SldWorks.Feature
3956

40-
Next
41-
42-
GetBodyCopies = vBodies
43-
44-
End Function
45-
46-
Sub CreateFeaturesForBodies(part As SldWorks.PartDoc, vBodies As Variant)
47-
48-
Dim i As Integer
57+
Set swFeat = part.CreateFeatureFromBody3(swBodyCopy, False, swCreateFeatureBodyOpts_e.swCreateFeatureBodySimplify)
58+
59+
If Not swFeat Is Nothing Then
60+
61+
Dim swFace As SldWorks.Face2
62+
Set swFace = swFeat.GetFaces()(0)
63+
64+
Dim swReplacedBody As SldWorks.Body2
65+
Set swReplacedBody = swFace.GetBody
66+
67+
swReplacedBody.HideBody False = swBody.Visible
68+
69+
Else
70+
Err.Raise vbError, "", "Failed to create feature for a body " & swBody.Name
71+
End If
72+
73+
Next
4974

50-
For i = 0 To UBound(vBodies)
51-
Dim swBody As SldWorks.Body2
52-
Set swBody = vBodies(i)
53-
part.CreateFeatureFromBody3 swBody, False, swCreateFeatureBodyOpts_e.swCreateFeatureBodySimplify
54-
Next
75+
Else
76+
77+
Err.Raise vbError, "", "No bodies found"
78+
79+
End If
5580

5681
End Sub
5782

58-
Sub DeleteAllUserFeatures(model As SldWorks.ModelDoc2)
83+
Sub DeleteFeatures(model As SldWorks.ModelDoc2, feats As Variant)
5984

60-
SelectAllTopLevelUserFeatures model
61-
62-
model.Extension.DeleteSelection2 swDeleteSelectionOptions_e.swDelete_Children + swDeleteSelectionOptions_e.swDelete_Absorbed
85+
If model.Extension.MultiSelect2(feats, False, Nothing) = UBound(feats) + 1 Then
86+
model.Extension.DeleteSelection2 swDeleteSelectionOptions_e.swDelete_Children + swDeleteSelectionOptions_e.swDelete_Absorbed
87+
Else
88+
Err.Raise vbError, "", "Failed to select user features"
89+
End If
6390

6491
End Sub
6592

66-
Sub SelectAllTopLevelUserFeatures(model As SldWorks.ModelDoc2)
93+
Function GetAllTopLevelUserFeatures(model As SldWorks.ModelDoc2) As Variant
6794

68-
model.ClearSelection2 True
95+
Dim swUserFeats() As SldWorks.Feature
6996

7097
Dim swFeat As SldWorks.Feature
7198

7299
Set swFeat = model.FirstFeature
73100

74-
Dim selectFeat As Boolean
75-
selectFeat = False
101+
Dim isUserFeat As Boolean
102+
isUserFeat = False
76103

77104
While Not swFeat Is Nothing
78105

79-
If selectFeat Then
80-
swFeat.Select2 True, -1
106+
If isUserFeat Then
107+
108+
If (Not swUserFeats) = -1 Then
109+
ReDim swUserFeats(0)
110+
Else
111+
ReDim Preserve swUserFeats(UBound(swUserFeats) + 1)
112+
End If
113+
114+
Set swUserFeats(UBound(swUserFeats)) = swFeat
115+
81116
Else
82117
If swFeat.GetTypeName2() = "OriginProfileFeature" Then
83-
selectFeat = True
118+
isUserFeat = True
84119
End If
85120
End If
86121

87122
Set swFeat = swFeat.GetNextFeature
88123

89124
Wend
90125

91-
End Sub
126+
If (Not swUserFeats) = -1 Then
127+
GetAllTopLevelUserFeatures = Empty
128+
Else
129+
GetAllTopLevelUserFeatures = swUserFeats
130+
End If
131+
132+
End Function

solidworks-api/document/features-manager/defeature-part/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ description: Macro to convert all features in part to dumb solids (defeature par
55
image: part-feature-tree-defeatured.png
66
labels: [defeature,parasolid]
77
---
8-
This macro emulates the functionality of [Defeature for Part](https://help.solidworks.com/2018/english/solidworks/sldworks/c_defeature_for_parts.htm) but not using it directly.
8+
This VBA macro defeatures the active SOLIDWORKS part. Unlike the [Defeature for Part](https://help.solidworks.com/2018/english/solidworks/sldworks/c_defeature_for_parts.htm) functionality, this macro preserves the original geometry and does not simplify it.
99

10-
Macro copies all visible solid and surface bodies, deletes all user features and imports the copied bodies using SOLIDWORKS API.
10+
Macro copies all solid and surface bodies, deletes all user features and imports the copied bodies using SOLIDWORKS API. Macro will preserve the hidden flag from the original bodies.
1111

1212
**Before:**
1313

0 commit comments

Comments
 (0)