MoI discussion forum
MoI discussion forum

Full Version: delete / remove face of a BRep in a script

From: pressure (PEER)
22 Feb 2023   [#1]
I'm trying to delete one face of a solid and then do something else with the resulting object in a script. I don't understand how the delete factory works.

I can successfully delete a selected face and be left with an object that looks like the original solid less one face:

code:
var faces = moi.geometryDatabase.getSelectedObjects().getFaces();

// delete original face
var factory = moi.command.createFactory( 'delete' );
factory.setInput( 0, faces );
var facelessParent = factory.commit();


But if I try to get the new object that exists after deleting the face I run into trouble:

code:
var faces = moi.geometryDatabase.getSelectedObjects().getFaces();

// delete original face
var factory = moi.command.createFactory( 'delete' );
factory.setInput( 0, faces );
var facelessParent = factory.calculate();
moi.geometryDatabase.addObjects( facelessParent );


Specifically, the combination of .calculate() and .addObjects ( object ) results in 2 objects existing: a solid that looks like the original solid and a solid less one face. I can do what I want with the new object facelessParent, but I have to do another deletion to get rid of the solid that remains in the scene. I thought that maybe the delete factory is returning an ObjectList with more than 1 object, but that doesn't seem to be the case.

What's the right way to delete a face of a BRep and then doing something additional to the new BRep?

- Peer
From: bemfarmer
22 Feb 2023   [#2] In reply to [#1]
The original [object] remains unchanged in the geometry database.
Apply "Remove" to it.

I do not remember the exact syntax of gd.remove([Edit: The top level object containing faces]). I used Remove() in chain script.
It was used in Max's scripts. I await Michael's response:-)

- Brian

Thank you for your code examples.
From: pressure (PEER)
22 Feb 2023   [#3] In reply to [#2]
Hi Brian,

Is there some trick to object removal? I tried:

code:
var objList = moi.geometryDatabase.getSelectedObjects();
moi.geometryDatabase.removeObjects(objList);


And that works fine for a standalone selected object, but it doesn't seem to do anything if just a face of a solid is selected.

- Peer
From: bemfarmer
22 Feb 2023   [#4] In reply to [#3]
Hi PEER,

I think that the Name of the object in the GD needs to be obtained, in order to remove() it. [EDIT, this statement is at best inaccurate, or misleading and incomplete, or incorrect. see Michael's answer. The parent object needs to be identified or selected]

Due to my ignorance, and to avoid causing confusion, no more comment :-)
[Deleted bad question/statement]

- Brian
From: bemfarmer
22 Feb 2023   [#5] In reply to [#4]
Existing script:
DeletePoints

Ctrl+P script:var gd = moi.geometryDatabase; var pts = gd.getObjects().getPoints(); gd.removeObjects( pts );

****
[Edit: In this case the points are the top level objects...]

[Deleted other false statement]

- Brian

It seems that scripting is hard. Logic must follow narrow guidelines. Common sense logic only goes so far.
I'm thinking that your code is not quite done in the proper ways?

The geometry database is an area of computer memory which contains a "spreadsheet" structure of compound "lists and arrays" in specific formats, of the
cad data, which can be displayed on the screen. Entities created in scripts need to be added to the gd in order to be displayed, and often to be utilized by other commands, and become part of the .3dm file.

OK, now I say no more, due to my ignorance. :-)

[I edited some of my incorrect statements]
From: Michael Gibson
22 Feb 2023   [#6] In reply to [#1]
Hi Peer,

re:
> Specifically, the combination of .calculate() and .addObjects ( object ) results in 2 objects existing: a solid that
> looks like the original solid and a solid less one face.

Basically objects in the geometry database are immutable, to modify an object a geometry factory will create a new object and the old object will need to be removed from the geometry database.

This cycle of generating a new object and removing the old object is handled automatically by the factory.update() and factory.commit() mechanisms that regular commands use.

It can be useful for a script to handle it manually though, which is done by using factory.calculate() instead of factory.update()/commit() - unlike update/commit the calculate() function will generate an output object but not automatically add it to the geometry database or remove any input objects from the geometry database.


> I can do what I want with the new object facelessParent, but I have to do another deletion to get rid of the solid that remains in the scene.

You'll need to remove the old object from the geometry database if you don't want it to be around anymore. But note that you'll need to remove the top level parent object not face sub-objects. Faces don't exist on their own, they are a subordinate part of a brep.

- Michael
From: Michael Gibson
22 Feb 2023   [#7] In reply to [#3]
Hi Peer,

> And that works fine for a standalone selected object, but it doesn't seem to do anything
> if just a face of a solid is selected.

You can't remove a sub-object from the geometry database, you can only remove a top level object from the geometry database.

You can get the top level object that a sub-object is contained inside of by using obj.getTopLevelParent();

- Michael
From: pressure (PEER)
22 Feb 2023   [#8] In reply to [#6]
Thanks Michael for your fast and thorough answer! Needing to separately remove the input object makes sense now. And thanks for pointing out that I need to remove the top level parent object. That probably explains an issue I saw when testing my script on a solid contained by a Group object.

- Peer
From: bemfarmer
22 Feb 2023   [#9]
Thank you Michael and Peer for the improvement to understanding. I edited my posts to remove or modify my worst statements.

- Brian