Page 1 of 1
Is this macro possible: delete feature tree & insert as 'imported' geometry
Posted: Fri Jun 03, 2022 11:49 am
by berg_lauritz
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'.
- 2022-06-03 10_39_05-Window.png (13.96 KiB) Viewed 1562 times
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
Re: Is this macro possible: delete feature tree & insert as 'imported' geometry
Posted: Fri Jun 03, 2022 12:29 pm
by SPerman
There may be a more elegant solution, but you could always have the macro do the export / import automatically.
Re: Is this macro possible: delete feature tree & insert as 'imported' geometry
Posted: Fri Jun 03, 2022 12:49 pm
by berg_lauritz
SPerman wrote: ↑Fri Jun 03, 2022 12:29 pm
There may be a more elegant solution, but you could always have the macro do the export / import automatically.
In the same part though? That's the key point of it.
Re: Is this macro possible: delete feature tree & insert as 'imported' geometry
Posted: Fri Jun 03, 2022 1:02 pm
by bnemec
berg_lauritz wrote: ↑Fri Jun 03, 2022 12:49 pm
In the same part though? That's the key point of it.
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
Posted: Fri Jun 03, 2022 1:08 pm
by SPerman
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.
Re: Is this macro possible: delete feature tree & insert as 'imported' geometry
Posted: Fri Jun 03, 2022 1:13 pm
by Austin Schukar
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.
Re: Is this macro possible: delete feature tree & insert as 'imported' geometry
Posted: Fri Jun 03, 2022 1:21 pm
by berg_lauritz
@Austin Schukar , @SPerman
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
Posted: Fri Jun 03, 2022 1:33 pm
by JSculley
I think this is what you are after. Here's the C# macro code:
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;
}
Here's a before model:
and then after:
Re: Is this macro possible: delete feature tree & insert as 'imported' geometry
Posted: Fri Jun 03, 2022 1:42 pm
by berg_lauritz
JSculley wrote: ↑Fri Jun 03, 2022 1:33 pm
I think this is what you are after. Here's the C# macro code:
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;
}
Here's a before model:
image.png
and then after:
image.png
Can someone translate that into VBA please? Yes. This is what I'm after!
P.S.: I just saw the name of the part. Very nice!
Re: Is this macro possible: delete feature tree & insert as 'imported' geometry
Posted: Fri Jun 03, 2022 2:01 pm
by JSculley
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
Posted: Fri Jun 03, 2022 2:05 pm
by berg_lauritz
JSculley wrote: ↑Fri Jun 03, 2022 2:01 pm
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
I will adjust my workflow and shamelessly plug this macro everywhere if you allow me to?
Re: Is this macro possible: delete feature tree & insert as 'imported' geometry
Posted: Fri Jun 03, 2022 2:24 pm
by JSculley
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?
Sure. Just be aware that minimal testing was done.