bernhard-42 / vscode-ocp-cad-viewer

A viewer for OCP based Code-CAD (CadQuery, build123d) integrated into VS Code
Apache License 2.0
120 stars 11 forks source link

[Question/Feature request] Can there be an interaction of selected objects in the viewer and build123d? #126

Open SuperMechaDeathChrist opened 4 days ago

SuperMechaDeathChrist commented 4 days ago

I was wondering if it is possible to, for example, select a face in the viewer and based on its identifier get the face and perform actions on the part (in the code that actually builds the part runing in the python interpreter).

I think it is confusing to explain, but basically i was experimenting with running ocp_vscode in standalone mode and i noticed that when i select a face, a request in the server is registered like this:

Data sent PropertiesResponse(type='backend_response', center_info='/Group/Solid/faces/faces_1 : Reference point has been taken as the center of the geometry', subtype='tool_response', tool_type='PropertiesMeasurement', center=(0.0, -0.0, 2.0), vertex_coords=(0.0, -0.0, 2.0), length=2.2, width=0.0, area=84.32, volume=None, radius=None, radius2=None, geom_type='PLANE')

I want to be able to do something like this:

This will allow for a more interactive approach (like using a full CAD software with an user interface), eliminating the need that, for example, sorting a list of faces by their coordinates in order to perform something as simple as selecting a top face.

What if i have an object with a lot of faces with different angles, and i want to select a face that is neither top nor bottom in any coordinate? Selecting it will be a mathematical challenge in itself.

Something like a button to perform an action like "copy selected" and functions to get these objects would be an absolute improvement in the user experience.

Please let me know if something like this is supported already or it can be implemented.

dalibor-frivaldsky commented 2 days ago

For such a functionality to work properly, the face identifier (and any other object type interactive in this way) would need to be given an identifier that is consistent (or to some extent consistent) across multiple executions of the code.

For examle, if the unique ID of the face changes with every execution of the code (let's say by generating a UUID each time), your code face = get_face(ID='....') would not work at all. The ID would identify the selected face only for the render your are selecting from, not for any subsequent re-renders.

The example of using some path to the face as an identifier would work only for as long as the geometry stays unchanged or the changes coincidentally lead to the selected face still having the same path. Which may be impossible to guarantee.

There may be some ways how to generate reasonably consistent identifiers for geometries, someone else would have to pitch in here, though. But I would say this would first need to be done inside build123d, if it's desirable.

For your usecase, for simple things like selecting the "top" face, you can write a small function that would do that instead of typing out the filter and sort every time. For the more complex scenario, I would say you are better off storing the face you want to edit later in a variable at the time it is created (supposedly before the rest of the other complex geometry exists) and then instead of selecting it anew, you start from that variable. What I think could be helpful in this case is the ability to highlight the code in the code editor that created or modified a face selected in the view.

bernhard-42 commented 1 day ago

thanks @dalibor-frivaldsky, I fully agree with what you said. This has been discussed several times on discord, but with no actionable result. The point is, that in build123d or cadquery you can run the code with changes again and again. So an identified object in one run can have a totally different id in the next run. At the end, build123d and cadquery rely on OCP, which relies on opencascade. In opencascade one can enumerate e.g. the edges of a given face or the faces of a given solid in a repeatable way as long as the objects are not modified (opc-tessellate does this), but this doesn't hold after modifying an object.