interactive selection of STL model triangles and edit ASCII STL file

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
sps
Posts: 22
Joined: Thu Aug 31, 2017 2:15 pm

interactive selection of STL model triangles and edit ASCII STL file

Post by sps »

I want to create a system that imports, edits and exports ASCII STL files (CAD file). Let me explain what I need and so what I mean as editing an STL file. I want to use an example. Consider the following STL file:

solid my_created_solid
facet normal 1.217327e-001 -6.162551e-002 -9.906480e-001
outer loop
vertex 9.211285e-002 8.592563e-002 1.910523e-001
vertex 9.977442e-002 8.592563e-002 1.921054e-001
vertex 9.954187e-002 8.500294e-002 1.921164e-001
endloop
endfacet
facet normal 1.519745e-001 -3.081203e-002 -9.879041e-001
outer loop
vertex 9.954187e-002 8.500294e-002 1.921164e-001
vertex 9.210512e-002 8.500294e-002 1.910942e-001
vertex 9.211285e-002 8.592563e-002 1.910523e-001
endloop
endfacet
endsolid

If you know the structure of a STL file, in this specific case, it is composed of two triangles (2 lists of points) and their relative normals. What I need is the GUI that let me pick some specific triangles and create a file like this:

solid I_part
facet normal 1.217327e-001 -6.162551e-002 -9.906480e-001
outer loop
vertex 9.211285e-002 8.592563e-002 1.910523e-001
vertex 9.977442e-002 8.592563e-002 1.921054e-001
vertex 9.954187e-002 8.500294e-002 1.921164e-001
endloop
endfacet
endsolid

solid II_part
facet normal 1.519745e-001 -3.081203e-002 -9.879041e-001
outer loop
vertex 9.954187e-002 8.500294e-002 1.921164e-001
vertex 9.210512e-002 8.500294e-002 1.910942e-001
vertex 9.211285e-002 8.592563e-002 1.910523e-001
endloop
endfacet
endsolid

Practically I need to select the triangles to put in I_part and those in II_part by mouse clicking.

So I'm thinking to use Freecad GUI to do that. Is it possible to select just some triangles? Could you suggest which functions/classes it uses to return the selected triangles? Could you give me some instructions or suggestions to start this applications? It's a research work, so I'm covering all useful information to get it easier. I used in the past FreeCAD for just small CAD editing, but never coded with it.
wmayer
Founder
Posts: 20309
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: interactive selection of STL model triangles and edit ASCII STL file

Post by wmayer »

solid I_part
facet normal 1.217327e-001 -6.162551e-002 -9.906480e-001
outer loop
vertex 9.211285e-002 8.592563e-002 1.910523e-001
vertex 9.977442e-002 8.592563e-002 1.921054e-001
vertex 9.954187e-002 8.500294e-002 1.921164e-001
endloop
endfacet
endsolid

solid II_part
facet normal 1.519745e-001 -3.081203e-002 -9.879041e-001
outer loop
vertex 9.954187e-002 8.500294e-002 1.921164e-001
vertex 9.210512e-002 8.500294e-002 1.910942e-001
vertex 9.211285e-002 8.592563e-002 1.910523e-001
endloop
endfacet
endsolid
Is it intended to write out several solids into a single STL file? I know that some applications do this too but I wonder if this is covered by the format specification.
So I'm thinking to use Freecad GUI to do that. Is it possible to select just some triangles? Could you suggest which functions/classes it uses to return the selected triangles?
In which language? C++ or Python?
sps
Posts: 22
Joined: Thu Aug 31, 2017 2:15 pm

Re: interactive selection of STL model triangles and edit ASCII STL file

Post by sps »

wmayer wrote: Thu Aug 31, 2017 6:33 pm I know that some applications do this too but I wonder if this is covered by the format specification.
Which software does this? I think it is not covered by the format specifications but it's not a my problem
wmayer wrote: Thu Aug 31, 2017 6:33 pm]
In which language? C++ or Python?
What you want
I want to understand the possibility and how to do that
wmayer
Founder
Posts: 20309
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: interactive selection of STL model triangles and edit ASCII STL file

Post by wmayer »

Which software does this? I think it is not covered by the format specifications but it's not a my problem
I was unsure about this since you wrote solid I_part and solid II_part in one block and that's why I asked. About the SW I don't know which one this does but in the past I saw Ascii STL files with multiple solids definition.
What you want
I want to understand the possibility and how to do that
OK, then I explain it in C++.
Here you will find an example. What you basically need is a C++ function with this interface:

void myCallbackFunc(void * ud, SoEventCallback * n)

You then have to register it for a certain event. In your case a mouse-click event will be fine.

Code: Select all

    Gui::Document* doc = Gui::Application::Instance->activeDocument();
    Gui::View3DInventor* view = static_cast<Gui::View3DInventor*>(doc->getActiveView());
    if (view) {
        Gui::View3DInventorViewer* viewer = view->getViewer();
        viewer->addEventCallback(SoMouseButtonEvent::getClassTypeId(), myCallbackFunc);
     }
Inside myCallbackFunc you get the SoEventCallback argument from which you obtain the click-point. If you didn't click into an empty space this should be a pointer different to null.

From this pointer you get an SoDetail pointer where you have to verify that it's an SoFaceDetail. If the test succeeds you get an index number which refers to the triangle in the mesh data structure. An example how to access this triangle you will find here.
sps
Posts: 22
Joined: Thu Aug 31, 2017 2:15 pm

Re: interactive selection of STL model triangles and edit ASCII STL file

Post by sps »

Thanks for your useful information
Now I'm sure that I can do what I want with FreeCAD. ;)

But now I need to understand how to create my first FreeCAD application.

Could please guide me in download, install correctly the FreeCAD library and dependencies, and how to create my first application with it?

After that I'll start to study classes and functions to create the application I need.

Waiting for your reply...
Thanks again!
wmayer
Founder
Posts: 20309
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: interactive selection of STL model triangles and edit ASCII STL file

Post by wmayer »

Could please guide me in download, install correctly the FreeCAD library and dependencies, and how to create my first application with it?
In order to do this you need to build FreeCAD directly from the source code. Dependent on your OS one of the following links is what you should study:
https://www.freecadweb.org/wiki/CompileOnWindows
https://www.freecadweb.org/wiki/CompileOnUnix
https://www.freecadweb.org/wiki/CompileOnMac

But maybe it's easier for you to do this in Python because therefore you can use your installed FreeCAD version and write a little macro. The idea is basically the same, it's just the syntax that is different.

Code: Select all

list_of_indexes = []

def myCallbackFunc(event_cb):
    event = event_cb.getEvent()
    if event.getButton() == coin.SoMouseButtonEvent.BUTTON1 and event.getState() == coin.SoMouseButtonEvent.DOWN:
        pp = event_cb.getPickedPoint()
        if not pp is None:
            detail = pp.getDetail()
            if detail.isOfType(coin.SoFaceDetail.getClassTypeId()):
                face_detail = coin.cast(detail, str(detail.getTypeId().getName()))
                index = face_detail.getFaceIndex()
                global list_of_ibdexes
                list_of_indexes.append(index)
            

cbfunc = Gui.ActiveDocument.ActiveView.addEventCallbackPivy(coin.SoMouseButtonEvent.getClassTypeId(), myCallbackFunc)
# click the triangles you need
# ...

Gui.ActiveDocument.ActiveView.removeEventCallbackPivy(coin.SoMouseButtonEvent.getClassTypeId(), cbfunc)


mesh = App.ActiveDocument.ActiveObject.Mesh
submesh=mesh.meshFromSegment(list_of_indexes)
submesh.write("mymesh.stl","AST")
The code is kept as simple as possible and only works if a single mesh is loaded. If several meshes are in the same document you have to add extra code to check which index belongs to which mesh.
sps
Posts: 22
Joined: Thu Aug 31, 2017 2:15 pm

Re: interactive selection of STL model triangles and edit ASCII STL file

Post by sps »

Actually I'm a bit confused

What it's not clear is if I need to build FreeCAD from the source code or just download the release version and do the script in Python?
I've understood that they are two different equivalent options.
If it is the second option valid (install release version and create a script in python), how to use that? Is there a guide about how to use Python script in the FreeCAD GUI for released version?


Thanks
wmayer
Founder
Posts: 20309
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: interactive selection of STL model triangles and edit ASCII STL file

Post by wmayer »

What it's not clear is if I need to build FreeCAD from the source code or just download the release version and do the script in Python?
If you use Python it suffices to use the release version. If you want to use C++ you have to get the source code and build FreeCAD from there. Then you have to write your own workbench and therefore you need the header files and library files in order to build your code.
If it is the second option valid (install release version and create a script in python), how to use that? Is there a guide about how to use Python script in the FreeCAD GUI for released version?
Maybe somewhere in the Wiki. The easiest is to put the script into the macro folder (see the destination folder when opening Macro > Macros...). Then under Tools > Customize > Macro tab you can associate the macro with a command which can be added to custom toolbars.

Edit:
https://www.freecadweb.org/wiki/Macros
https://www.freecadweb.org/wiki/Interface_Customization
sps
Posts: 22
Joined: Thu Aug 31, 2017 2:15 pm

Re: interactive selection of STL model triangles and edit ASCII STL file

Post by sps »

Thanks a lot

I want to try both the solutions to experiment the best one.
About the creation of macros I'm stuck in display mesh on FreeCAD. What I mean is:
  1. I import the STL file
  2. I create the macro with your updated code
  3. The problem is that I don't see the mesh on the 3D navigation panel so I cannot see the triangles, plus how to activate the python command line editor?
Sorry for these elementary questions but I'm very new to FreeCAD.

Moreover I tried to install source of FreeCAD.

I got the following problems:

Code: Select all

sudo apt install build-essential cmake python python-matplotlib libtool libcoin80-dev libsoqt4-dev libxerces-c-dev libboost-dev libboost-filesystem-dev libboost-regex-dev libboost-program-options-devlibboost-signals-dev libboost-thread-dev libboost-python-dev libqt4-dev libqt4-opengl-dev qt4-dev-tools python-dev python-pyside pyside-tools libeigen3-dev libqtwebkit-dev libshiboken-dev libpyside-dev libode-dev swig libzipios++-dev libfreetype6 libfreetype6-dev liboce-foundation-dev liboce-modeling-dev liboce-ocaf-dev liboce-visualization-dev liboce-ocaf-lite-dev libsimage-dev checkinstall python-pivy python-qt4 doxygen libspnav-dev oce-draw liboce-foundation-dev liboce-foundation10 liboce-foundation8 liboce-modeling-dev liboce-modeling10 liboce-modeling8 liboce-ocaf-dev liboce-ocaf-lite-dev liboce-ocaf-lite10 liboce-ocaf-lite8 liboce-ocaf10 liboce-ocaf8 liboce-visualization-dev liboce-visualization10 liboce-visualization8 libmedc-dev libvtk6-dev libproj-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package liboce-foundation10
E: Unable to locate package liboce-modeling10
E: Unable to locate package liboce-ocaf-lite10
E: Unable to locate package liboce-ocaf10
E: Unable to locate package liboce-visualization10
What's the problem with those libraries?
Is the command above enough to completely install all dependencies on Ubuntu 14.04 machine?


Thanks
peterl94
Veteran
Posts: 1001
Joined: Thu May 23, 2013 7:31 pm
Location: United States

Re: interactive selection of STL model triangles and edit ASCII STL file

Post by peterl94 »

sps wrote: Tue Sep 05, 2017 9:13 pm E: Unable to locate package liboce-foundation10
E: Unable to locate package liboce-modeling10
E: Unable to locate package liboce-ocaf-lite10
E: Unable to locate package liboce-ocaf10
E: Unable to locate package liboce-visualization10
Those are the names in 16.04 if I'm not mistaken. Instead of specifying the oce packages individually, you can just install these packages:

Code: Select all

liboce-foundation-dev
liboce-modeling-dev
liboce-ocaf-dev
liboce-ocaf-lite-dev
liboce-visualization-dev 
Post Reply