Is this macro possible: delete feature tree & insert as 'imported' geometry
-
- Posts: 423
- Joined: Tue Mar 09, 2021 10:11 am
- x 439
- x 233
Is this macro possible: delete feature tree & insert as 'imported' geometry
This might seem a silly idea but if this macro is possible it would solve many problems for us (basically make synchronous modeling possible in a way...)
When you import i.e. a parasolid file there is only one 'feature' present. The body (or the bodies) which are named i.e. 'imported1'.
Is it possible to write a macro that does this for me? That basically takes the geometry of what I have already designed, erases the whole feature tree and puts only this feature in there? I don't care if it looses all face IDs etc.
I actually want to skip the route of having to go export & then import again. It's important to still use the same file if possible.
Does anybody have an idea if this is possible?
Edit: After so many responses I have to clarify something. It is important that the feature in the feature tree is the 'imported' feature. No other feature (converted etc.) gives you the possibility to automatically recognize features for you!
I feel like it should be possible somehow because you can MODIFY THE BODY i.e. re-order imported features (or delete them), modify them (& then delete the recognized features from the tree) or use a move/copy body and then delete the move/copy body (thank you @josh !) without it affecting the feature tree.
@artem , @gupta9665 , @peterbrinkhuis
When you import i.e. a parasolid file there is only one 'feature' present. The body (or the bodies) which are named i.e. 'imported1'.
Is it possible to write a macro that does this for me? That basically takes the geometry of what I have already designed, erases the whole feature tree and puts only this feature in there? I don't care if it looses all face IDs etc.
I actually want to skip the route of having to go export & then import again. It's important to still use the same file if possible.
Does anybody have an idea if this is possible?
Edit: After so many responses I have to clarify something. It is important that the feature in the feature tree is the 'imported' feature. No other feature (converted etc.) gives you the possibility to automatically recognize features for you!
I feel like it should be possible somehow because you can MODIFY THE BODY i.e. re-order imported features (or delete them), modify them (& then delete the recognized features from the tree) or use a move/copy body and then delete the move/copy body (thank you @josh !) without it affecting the feature tree.
@artem , @gupta9665 , @peterbrinkhuis
Code: Select all
Sub main()
Set swApp = Application.SldWorks
Set mDoc = swApp.ActiveDoc
Set pDoc = mDoc
vBodies = pDoc.GetBodies2(swBodyType_e.swSolidBody, True)
For Each vBody In vBodies
Dim nextBody As Body2
Set nextBody = vBody
Dim bodyCopy As Body2
Set bodyCopy = nextBody.Copy2(False)
vFeatures = nextBody.GetFeatures()
For Each vFeature In vFeatures
Dim nextFeature As Feature
Set nextFeature = vFeature
If Not InStr(nextFeature.Name, "Imported") Then
nextFeature.Select2 True, 0
End If
Next
pDoc.CreateFeatureFromBody3 bodyCopy, False, swCreateFeatureBodyOpts_e.swCreateFeatureBodyCheck
Next
Set mExt = mDoc.Extension
mExt.DeleteSelection2 (swDeleteSelectionOptions_e.swDelete_Absorbed + swDeleteSelectionOptions_e.swDelete_Children)
End Sub
Re: Is this macro possible: delete feature tree & insert as 'imported' geometry
There may be a more elegant solution, but you could always have the macro do the export / import automatically.
-
I may not have gone where I intended to go, but I think I have ended up where I needed to be. -Douglas Adams
I may not have gone where I intended to go, but I think I have ended up where I needed to be. -Douglas Adams
-
- Posts: 423
- Joined: Tue Mar 09, 2021 10:11 am
- x 439
- x 233
Re: Is this macro possible: delete feature tree & insert as 'imported' geometry
Is there a reason why you cannot overwrite the existing file with new file? As long as the process doesn't delete the file PDM will treat it as the same "File" (it keeps history and doc ID).
Re: Is this macro possible: delete feature tree & insert as 'imported' geometry
This is what I would try, but it is a bit convoluted.
1. Save the model to a temporary parasolid file
2. open the parasolid file and save as a temporary part file
3. delete all of the features from the tree
4. insert the temporary part into the original part. Insert solid bodies only, and break the link to the original part.
5. delete the no longer needed temporary files.
Hopefully someone knows of a way to do this without all of the hoop jumping.
1. Save the model to a temporary parasolid file
2. open the parasolid file and save as a temporary part file
3. delete all of the features from the tree
4. insert the temporary part into the original part. Insert solid bodies only, and break the link to the original part.
5. delete the no longer needed temporary files.
Hopefully someone knows of a way to do this without all of the hoop jumping.
-
I may not have gone where I intended to go, but I think I have ended up where I needed to be. -Douglas Adams
I may not have gone where I intended to go, but I think I have ended up where I needed to be. -Douglas Adams
-
- Posts: 98
- Joined: Thu Mar 18, 2021 11:19 am
- Location: St. Louis, MO
- x 288
- x 56
Re: Is this macro possible: delete feature tree & insert as 'imported' geometry
Do you only want selected bodies to be converted into the same feature tree?
If you want all bodies, one option is to use the Convert to Bodies tool (will save as new part); then as mentioned, overwrite the existing file.
If you want all bodies, one option is to use the Convert to Bodies tool (will save as new part); then as mentioned, overwrite the existing file.
Austin
-
- Posts: 423
- Joined: Tue Mar 09, 2021 10:11 am
- x 439
- x 233
Re: Is this macro possible: delete feature tree & insert as 'imported' geometry
@Austin Schukar , @SPerman
It needs to be the imported feature. I clarified that in the original post now.
It needs to be the imported feature. I clarified that in the original post now.
Re: Is this macro possible: delete feature tree & insert as 'imported' geometry
I think this is what you are after. Here's the C# macro code:
Here's a before model:
and then after:
Code: Select all
public void Main()
{
ModelDoc2 mDoc = swApp.ActiveDoc as ModelDoc2;
PartDoc pDoc = mDoc as PartDoc;
object[] bodyObjArray = pDoc.GetBodies2((int)swBodyType_e.swSolidBody, true) as object[];
foreach (object nextBodyObj in bodyObjArray)
{
Body2 nextBody = nextBodyObj as Body2;
Body2 bodyCopy = nextBody.Copy2(false) as Body2;
object[] featureObjArray = nextBody.GetFeatures() as object[];
foreach (object featObj in featureObjArray)
{
Feature nextFeature = featObj as Feature;
if (nextFeature.Name.Contains("Imported")) { continue; }
nextFeature.Select2(true, 0);
}
pDoc.CreateFeatureFromBody3(bodyCopy, false, (int)swCreateFeatureBodyOpts_e.swCreateFeatureBodyCheck);
}
ModelDocExtension mExt = mDoc.Extension;
mExt.DeleteSelection2((int)(swDeleteSelectionOptions_e.swDelete_Absorbed | swDeleteSelectionOptions_e.swDelete_Children));
return;
}
-
- Posts: 423
- Joined: Tue Mar 09, 2021 10:11 am
- x 439
- x 233
Re: Is this macro possible: delete feature tree & insert as 'imported' geometry
Can someone translate that into VBA please? Yes. This is what I'm after!JSculley wrote: ↑Fri Jun 03, 2022 1:33 pm I think this is what you are after. Here's the C# macro code:
Here's a before model:Code: Select all
public void Main() { ModelDoc2 mDoc = swApp.ActiveDoc as ModelDoc2; PartDoc pDoc = mDoc as PartDoc; object[] bodyObjArray = pDoc.GetBodies2((int)swBodyType_e.swSolidBody, true) as object[]; foreach (object nextBodyObj in bodyObjArray) { Body2 nextBody = nextBodyObj as Body2; Body2 bodyCopy = nextBody.Copy2(false) as Body2; object[] featureObjArray = nextBody.GetFeatures() as object[]; foreach (object featObj in featureObjArray) { Feature nextFeature = featObj as Feature; if (nextFeature.Name.Contains("Imported")) { continue; } nextFeature.Select2(true, 0); } pDoc.CreateFeatureFromBody3(bodyCopy, false, (int)swCreateFeatureBodyOpts_e.swCreateFeatureBodyCheck); } ModelDocExtension mExt = mDoc.Extension; mExt.DeleteSelection2((int)(swDeleteSelectionOptions_e.swDelete_Absorbed | swDeleteSelectionOptions_e.swDelete_Children)); return; }
image.png
and then after:
image.png
P.S.: I just saw the name of the part. Very nice!
Re: Is this macro possible: delete feature tree & insert as 'imported' geometry
Code: Select all
Sub main()
Set swApp = Application.SldWorks
Set mDoc = swApp.ActiveDoc
Set pDoc = mDoc
vBodies = pDoc.GetBodies2(swBodyType_e.swSolidBody, True)
For Each vBody In vBodies
Dim nextBody As Body2
Set nextBody = vBody
Dim bodyCopy As Body2
Set bodyCopy = nextBody.Copy2(False)
vFeatures = nextBody.GetFeatures()
For Each vFeature In vFeatures
Dim nextFeature As Feature
Set nextFeature = vFeature
If Not InStr(nextFeature.Name, "Imported") Then
nextFeature.Select2 True, 0
End If
Next
pDoc.CreateFeatureFromBody3 bodyCopy, False, swCreateFeatureBodyOpts_e.swCreateFeatureBodyCheck
Next
Set mExt = mDoc.Extension
mExt.DeleteSelection2 (swDeleteSelectionOptions_e.swDelete_Absorbed + swDeleteSelectionOptions_e.swDelete_Children)
End Sub
-
- Posts: 423
- Joined: Tue Mar 09, 2021 10:11 am
- x 439
- x 233
Re: Is this macro possible: delete feature tree & insert as 'imported' geometry
I will adjust my workflow and shamelessly plug this macro everywhere if you allow me to?JSculley wrote: ↑Fri Jun 03, 2022 2:01 pmCode: Select all
Sub main() Set swApp = Application.SldWorks Set mDoc = swApp.ActiveDoc Set pDoc = mDoc vBodies = pDoc.GetBodies2(swBodyType_e.swSolidBody, True) For Each vBody In vBodies Dim nextBody As Body2 Set nextBody = vBody Dim bodyCopy As Body2 Set bodyCopy = nextBody.Copy2(False) vFeatures = nextBody.GetFeatures() For Each vFeature In vFeatures Dim nextFeature As Feature Set nextFeature = vFeature If Not InStr(nextFeature.Name, "Imported") Then nextFeature.Select2 True, 0 End If Next pDoc.CreateFeatureFromBody3 bodyCopy, False, swCreateFeatureBodyOpts_e.swCreateFeatureBodyCheck Next Set mExt = mDoc.Extension mExt.DeleteSelection2 (swDeleteSelectionOptions_e.swDelete_Absorbed + swDeleteSelectionOptions_e.swDelete_Children) End Sub
Re: Is this macro possible: delete feature tree & insert as 'imported' geometry
Sure. Just be aware that minimal testing was done.berg_lauritz wrote: ↑Fri Jun 03, 2022 2:05 pm I will adjust my workflow and shamelessly plug this macro everywhere if you allow me to?