Is this macro possible: delete feature tree & insert as 'imported' geometry

Programming and macros
berg_lauritz
Posts: 423
Joined: Tue Mar 09, 2021 10:11 am
Answers: 6
x 439
x 233

Is this macro possible: delete feature tree & insert as 'imported' geometry

Unread post 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
2022-06-03 10_39_05-Window.png (13.96 KiB) Viewed 1561 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
by JSculley » 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
Go to full post
User avatar
SPerman
Posts: 2068
Joined: Wed Mar 17, 2021 4:24 pm
Answers: 14
x 2245
x 1889
Contact:

Re: Is this macro possible: delete feature tree & insert as 'imported' geometry

Unread post by SPerman »

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
berg_lauritz
Posts: 423
Joined: Tue Mar 09, 2021 10:11 am
Answers: 6
x 439
x 233

Re: Is this macro possible: delete feature tree & insert as 'imported' geometry

Unread post 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.
User avatar
bnemec
Posts: 1953
Joined: Tue Mar 09, 2021 9:22 am
Answers: 10
Location: Wisconsin USA
x 2559
x 1410

Re: Is this macro possible: delete feature tree & insert as 'imported' geometry

Unread post 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).
User avatar
SPerman
Posts: 2068
Joined: Wed Mar 17, 2021 4:24 pm
Answers: 14
x 2245
x 1889
Contact:

Re: Is this macro possible: delete feature tree & insert as 'imported' geometry

Unread post 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.
-
I may not have gone where I intended to go, but I think I have ended up where I needed to be. -Douglas Adams
Austin Schukar
Posts: 98
Joined: Thu Mar 18, 2021 11:19 am
Answers: 1
Location: St. Louis, MO
x 288
x 56

Re: Is this macro possible: delete feature tree & insert as 'imported' geometry

Unread post 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.
Attachments
converttobodiesNew.JPG
converttobodies.jpg
Austin
berg_lauritz
Posts: 423
Joined: Tue Mar 09, 2021 10:11 am
Answers: 6
x 439
x 233

Re: Is this macro possible: delete feature tree & insert as 'imported' geometry

Unread post by berg_lauritz »

@Austin Schukar , @SPerman

It needs to be the imported feature. I clarified that in the original post now.
User avatar
JSculley
Posts: 648
Joined: Tue May 04, 2021 7:28 am
Answers: 55
x 9
x 884

Re: Is this macro possible: delete feature tree & insert as 'imported' geometry

Unread post 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:
image.png
and then after:
image.png
berg_lauritz
Posts: 423
Joined: Tue Mar 09, 2021 10:11 am
Answers: 6
x 439
x 233

Re: Is this macro possible: delete feature tree & insert as 'imported' geometry

Unread post 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!
User avatar
JSculley
Posts: 648
Joined: Tue May 04, 2021 7:28 am
Answers: 55
x 9
x 884

Re: Is this macro possible: delete feature tree & insert as 'imported' geometry

Unread post 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
berg_lauritz
Posts: 423
Joined: Tue Mar 09, 2021 10:11 am
Answers: 6
x 439
x 233

Re: Is this macro possible: delete feature tree & insert as 'imported' geometry

Unread post 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?
User avatar
JSculley
Posts: 648
Joined: Tue May 04, 2021 7:28 am
Answers: 55
x 9
x 884

Re: Is this macro possible: delete feature tree & insert as 'imported' geometry

Unread post 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.
Post Reply