Using MeshFeature in C++

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!
toral
Posts: 11
Joined: Fri May 18, 2018 1:21 pm

Using MeshFeature in C++

Post by toral »

Hello,

When I include "Mod\Mesh\Gui\PreCompiled.h" and "Mod/Mesh/App/MeshFeature.h" to my project to use MeshFeature class, the objects in my document disappear. (C++)

Any ideas?
toral
Posts: 11
Joined: Fri May 18, 2018 1:21 pm

Re: Using MeshFeature in C++

Post by toral »

const MeshCore::MeshFacetArray& facets = mf->Mesh.getValuePtr()->getKernel().GetFacets();

when i comment out this line from my function, everything works fine. This function is not even called when the problem happens.

Any help will be appreciated.
wmayer
Founder
Posts: 20319
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Using MeshFeature in C++

Post by wmayer »

When I include "Mod\Mesh\Gui\PreCompiled.h" and
This is already wrong. A pre-compiled header must be included only from .cpp files of the same module. And it must not be included inside header files because it usually contains some module-specific defines and thus might break dependent modules (like yours).
const MeshCore::MeshFacetArray& facets = mf->Mesh.getValuePtr()->getKernel().GetFacets();

when i comment out this line from my function, everything works fine. This function is not even called when the problem happens.
Without a single word about how you use the Mesh stuff or some code snippets it's impossible to say why things don't work as expected.
toral
Posts: 11
Joined: Fri May 18, 2018 1:21 pm

Re: Using MeshFeature in C++

Post by toral »

Thank you for your answer,

I was talking about my cpp file. SceneController.cpp in this case. I did not include anything related with FreeCAD in my header file. I only forward declared MDIView for;

Code: Select all

 void onWindowStateChanged(MDIView*);

I only include PreCompiled.h for MeshExport dll import definition. When I define it as;

Code: Select all

define MeshExport  __declspec(dllimport)
I still encounter the same problem.

So this is my only function that I use MeshFeature;

Code: Select all

void SceneController::getVal()
{
	App::DocumentObject* obj = App::GetApplication().getActiveDocument()->getObject("myimportedmesh");
	Mesh::Feature* mf = static_cast<Mesh::Feature*>(obj);
	const MeshCore::MeshFacetArray& facets = mf->Mesh.getValuePtr()->getKernel().GetFacets();
}
I just want to test it to get Mesh vertices from FreeCAD mesh objects. Again, when I comment out

Code: Select all

const MeshCore::MeshFacetArray& facets = mf->Mesh.getValuePtr()->getKernel().GetFacets();
this line everything works fine. But when I try to get facets I only see the background in my document. This function is not even called, so I thought including MeshFeature.h cause this.

Sorry for any mistakes. Thank you again.


EDIT:
Basically, how can i use MeshFeature correctly in my class? (I already add additional include directories, linked Mesh_d.lib and using Mesh_d.pyd and tried to include <Mod/Mesh/App/MeshFeature.h> in my class)

Note: I am using QT5.6.0 and PySide2
wmayer
Founder
Posts: 20319
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Using MeshFeature in C++

Post by wmayer »

I only include PreCompiled.h for MeshExport dll import definition. When I define it as;
In case SceneController.cpp is part of your own module and not MeshGui you should not use the PreCompiled.h of MeshGui (in case you do this) but write your own PreCompiled.h.
void SceneController::getVal()
{
App::DocumentObject* obj = App::GetApplication().getActiveDocument()->getObject("myimportedmesh");
Mesh::Feature* mf = static_cast<Mesh::Feature*>(obj);
const MeshCore::MeshFacetArray& facets = mf->Mesh.getValuePtr()->getKernel().GetFacets();
}
Here you have to add some security checks so that a static_cast is safe as otherwise the behaviour is undefined. When you get back the object by "getObject" you have to make sure that it's not null and that it's really of the expected type. Alternatively you can use a dynamic_cast and check if "mf" is null or not.
const MeshCore::MeshFacetArray& facets = mf->Mesh.getValuePtr()->getKernel().GetFacets();
As long as it's guaranteed that "mf" is Mesh::Feature this line is harmless and it doesn't modify the mesh data.
wmayer
Founder
Posts: 20319
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Using MeshFeature in C++

Post by wmayer »

EDIT:
Basically, how can i use MeshFeature correctly in my class? (I already add additional include directories, linked Mesh_d.lib and using Mesh_d.pyd and tried to include <Mod/Mesh/App/MeshFeature.h> in my class)
Assuming your module is called "MyModule" then check the following points:
  1. Inside the CMake write something like

    Code: Select all

    set(MyModule_LIBS
        Mesh   ### just write Mesh without any prefix or suffix. CMake will handle debug/release names automatically.
    )
    ...
    add_library(MyModule SHARED ${MyModule_SRCS})
    target_link_libraries(MyModule ${MyModule_LIBS})
    
  2. Create your own PreCompiled.h with at least this content:

    Code: Select all

    #ifndef __PRECOMPILED__
    #define __PRECOMPILED__
    
    #include <FCConfig.h>
    
    #ifdef FC_OS_WIN32
    # define MeshExport     __declspec(dllimport)
    #else // for Linux
    # define MeshExport   
    #endif
    
    #endif
    
    and use it as very first statement in your .cpp files
  3. After

    Code: Select all

    #include <PreCompiled.h>
    you can write

    Code: Select all

    #include <Mod/Mesh/App/MeshFeature.h>
  4. When accessing an object of the document make sure the object exists and that it's of the expected type.
User avatar
wandererfan
Veteran
Posts: 6326
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: Using MeshFeature in C++

Post by wandererfan »

toral wrote: Tue Aug 07, 2018 7:08 am I only include PreCompiled.h for MeshExport dll import definition. When I define it as;

Code: Select all

define MeshExport  __declspec(dllimport)
This code should be in your module's PreCompiled.h rather than including ...Mod/Mesh/App/PreCompiled.h in your cpp files.

From PartDesign/App/PreCompiled.h

Code: Select all

// Exporting of App classes
#ifdef FC_OS_WIN32
# define PartDesignExport __declspec(dllexport)
# define PartExport  __declspec(dllimport)
# define MeshExport     __declspec(dllimport)
#else // for Linux
# define PartDesignExport
# define PartExport 
# define MeshExport   
#endif
toral
Posts: 11
Joined: Fri May 18, 2018 1:21 pm

Re: Using MeshFeature in C++

Post by toral »

Now I did exactly how you explained. Problem still occurs when I even use;

Code: Select all

const Mesh::MeshObject* a = mf->Mesh.getValuePtr();
in my function. Scene dissapears even I do not call the function. When I delete this line and Clean Solution and rebuild again everything works fine.

Again, this line is not even called when problem occurs. I have no idea why this is happening. But somehow when the compiler sees this line something in FreeCAD document fails.


EDIT: Now when I try to import Part::Box to my scene it is rendered successfully. Only the meshes are failed to render.
toral
Posts: 11
Joined: Fri May 18, 2018 1:21 pm

Re: Using MeshFeature in C++

Post by toral »

Probably when I link Mesh_d.lib and solution use it the problem happens.

Thank you for your time.
wmayer
Founder
Posts: 20319
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Using MeshFeature in C++

Post by wmayer »

is it possible to upload your code or if not offer a stripped down version that shows this behaviour?
Post Reply