1

I just want to write two simple functions with opencascade to be called from a C# winform application: one for create a surface from points, one for get the points of the surface. I don't write in C++, but following opencascade samples and by documentation and peace of code I arrive to this:

OCCProxy.h

#pragma once

#ifdef OCCTPROXY_EXPORTS
#define OCCTPROXY_API __declspec(dllexport)
#else
#define OCCTPROXY_API __declspec(dllimport)
#endif

extern "C" OCCTPROXY_API void PtsToSrf(double points[][3], int ptsNumber);

extern "C" OCCTPROXY_API void GetSrfPoint(double u, double v, double& x, double& y, double& z);

OCCProxy.cpp

#include "GeomAbs_Shape.hxx"
#include "Geom_BSplineSurface.hxx"
#include "GeomAPI_PointsToBSplineSurface.hxx"
#include "gp_Pnt.hxx"
#include "NCollection_Mat4.hxx"
#include "OCCTProxy.h"
#include "pch.h"
#include "Standard_Handle.hxx"
#include "TColgp_Array2OfPnt.hxx"

//Approximated surface
Handle(Geom_BSplineSurface) srf; //line 12

//Create approximated surface from a list of point
void PtsToSrf(double points[][3], int ptsNumber)
{
    //convert double array points array     
    TColgp_Array2OfPnt pts(0, ptsNumber, 0, 0);
    for (int i = 0; i < ptsNumber; i++) 
        for (int j = 0; i < 3; i++)
        {
            gp_Pnt pt = gp_Pnt(
                points[i][0], points[i][1], points[i][2]);
            pts.SetValue(i, 0, pt);
        }   

    //approximate a BSpline surface passing through an array of points
    GeomAPI_PointsToBSplineSurface srf_approximator(   //line 28
        pts, 3, 8, GeomAbs_C2, 0.001);   //line 29
    srf = srf_approximator.Surface();   //line 30   
}

//Get approximated surface point
void GetSrfPoint(double u, double v,
    double& x, double& y, double& z)
{
    gp_Pnt p;
    srf->D0(u, v, p);   //line 38
    
    x = p.X();
    y = p.Y();
    z = p.Z();
}

I get the following list error in "OCCProxy.cpp":

C2065 'Geom_BSplineSurface': undeclared identifier [line 12]

C2923 'opencascade::handle': 'Geom_BSplineSurface' is not a valid template type argument for parameter 'T' [line 12]

C2133 'srf': unknown size [line 12]

C2512 'opencascade::handle': no appropriate default constructor available [line 12]

C2065 'GeomAPI_PointsToBSplineSurface': undeclared identifier [line 28]

C2146 syntax error: missing ';' before identifier 'srf_approximator' [line 28]

C2065 'GeomAbs_C2': undeclared identifier [line 29]

C3861 'srf_approximator': identifier not found [line 28]

C2065 'srf_approximator': undeclared identifier [line 30]

C2678 binary '->': no operator found which takes a left-hand operand of type 'opencascade::handle' (or there is no acceptable conversion) [line 38]

C2039 'D0': is not a member of 'opencascade::handle' [line 38]

Maybe they are very stupid problem but I can't solve it.

Thanks for any help

Giovanni

Edit - Definition of Geom_BSplineSurface

class Geom_BSplineSurface;
DEFINE_STANDARD_HANDLE(Geom_BSplineSurface, Geom_BoundedSurface)

class Geom_BSplineSurface : public Geom_BoundedSurface
{

public:

  Standard_EXPORT Geom_BSplineSurface(const TColgp_Array2OfPnt& Poles, const TColStd_Array1OfReal& UKnots, const TColStd_Array1OfReal& VKnots, const TColStd_Array1OfInteger& UMults, const TColStd_Array1OfInteger& VMults, const Standard_Integer UDegree, const Standard_Integer VDegree, const Standard_Boolean UPeriodic = Standard_False, const Standard_Boolean VPeriodic = Standard_False);

  Standard_EXPORT Geom_BSplineSurface(const TColgp_Array2OfPnt& Poles, const TColStd_Array2OfReal& Weights, const TColStd_Array1OfReal& UKnots, const TColStd_Array1OfReal& VKnots, const TColStd_Array1OfInteger& UMults, const TColStd_Array1OfInteger& VMults, const Standard_Integer UDegree, const Standard_Integer VDegree, const Standard_Boolean UPeriodic = Standard_False, const Standard_Boolean VPeriodic = Standard_False);

  ... methods

 Standard_EXPORT Handle(Geom_Geometry) Copy() const Standard_OVERRIDE;

  ... other methods And fields
}
Giovanni
  • 13
  • 5
  • If you open `Geom_BSplineSurface.hxx` how class `Geom_BSplineSurface` is defined? Can you post it? BTW, if you are using IDE, can you open `Geom_BSplineSurface.hxx` through IDE menu? For example in Visual Studio you could open by right clicking `Geom_BSplineSurface` in `#include "Geom_BSplineSurface.hxx"` and clicking on "Open Document "Geom_BSplineSurface.hxx"". –  Jul 07 '21 at 17:15
  • Yes, I can. It's a class inheriting from `Geom_BoundedSurface`, I post it on the question. Thank you for answering. – Giovanni Jul 08 '21 at 06:49
  • @Givovanni could you put the at least part till declaration of class `Geom_BSplineSurface` in question? –  Jul 08 '21 at 06:51
  • @Givovanni two things: 1. Everywhere I search I see people are using angular brackets "<"/">". 2. Number of included files _always_ seem to be very large. Usually that is required if you use forward declaration in header file which this seems to do. See here for example: https://github.com/xBimTeam/XbimGeometry/blob/master/Xbim.Geometry.Engine/OCC/src/ShapeCustom/ShapeCustom_Surface.cxx. Could you do these both things and see if it works? Also, how are you compiling? Visual Studio right? If yes, details such as version and so on? –  Jul 08 '21 at 07:14
  • Using angular brackets doesn't work for "OCCTProxy.h" and "pch.h" includes. I tried brackets for the others but nothing change. I don't know what other includes I've to add, I added the includes for each opencascade class I used. And I added the _Additional include directory_ for let the code find what it needs. I use VS2019 and compiling _Debug - Any CPU_ – Giovanni Jul 08 '21 at 08:03
  • sorry about that. Please update your listing\content of OCCProxy.cpp in question so that if others want to help, they will have latest code. Also, please tell me where can I find opencascade version you are using. Let me try to reproduce the problem. –  Jul 08 '21 at 08:14
  • Sorry, maybe I was not clear, I mean that the additional includes (and includes directory) are already in the code, so the code is not changed. For the brackets I think it's the same... I download opencascade here: https://dev.opencascade.org/release , source package, tgz archive. Thank you so much for your interest. – Giovanni Jul 08 '21 at 10:00

1 Answers1

1

FINALLY, I could replicate and resolve the problem on my computer.

The problem is in line #include "pch.h". This is precompiled header. It has to be included first. See here for the reason why: What is "pch.h" and why is it needed to be included as the first header file?

So just move #include "pch.h" as first line and you will be fine.

#include "pch.h"
#include <GeomAbs_Shape.hxx>
#include <Geom_BSplineSurface.hxx>
#include <GeomAPI_PointsToBSplineSurface.hxx>
#include "gp_Pnt.hxx"
#include "NCollection_Mat4.hxx"
#include "OCCProxy.h"
#include "Standard_Handle.hxx"
#include "TColgp_Array2OfPnt.hxx"




//Approximated surface
Handle(Geom_BSplineSurface) srf; //line 12

//Create approximated surface from a list of point
extern "C" OCCTPROXY_API void PtsToSrf(double points[][3], int ptsNumber)
{
    //convert double array points array     
    TColgp_Array2OfPnt pts(0, ptsNumber, 0, 0);
    for (int i = 0; i < ptsNumber; i++)
        for (int j = 0; i < 3; i++)
        {
            gp_Pnt pt = gp_Pnt(
                points[i][0], points[i][1], points[i][2]);
            pts.SetValue(i, 0, pt);
        }

    //approximate a BSpline surface passing through an array of points
    GeomAPI_PointsToBSplineSurface srf_approximator(   //line 28
        pts, 3, 8, GeomAbs_C2, 0.001);   //line 29
    srf = srf_approximator.Surface();   //line 30   
}
//
////Get approximated surface point
extern "C" OCCTPROXY_API void GetSrfPoint(double u, double v,
    double& x, double& y, double& z)
{
    gp_Pnt p;
    srf->D0(u, v, p);   //line 38

    x = p.X();
    y = p.Y();
    z = p.Z();
}
  • Thank you @vish for find this error, I wouldn't have thought of it. But now I'm getting Linker Tools Error (LNK2001 e LNK2019 in "OCCTProxy.obj" and LNK1120 in "OCCTProxy.dll") are you compiling differently from me? – Giovanni Jul 08 '21 at 13:06
  • What is the exact error? Please note, you will need to add relevant libs in Project -> properties -> Linker -> Input -> Additional Dependencies –  Jul 08 '21 at 13:07
  • e.g. `LNK2001 unresolved external symbol "public: virtual void __thiscall Standard_OutOfMemory::SetMessageString(char const * const)" (?SetMessageString@Standard_OutOfMemory@@UAEXQBD@Z)` – Giovanni Jul 08 '21 at 13:07
  • yes, see my comment above. The libs are located in `\vc14-64\opencascade-7.5.0\win64\vc14\lib`. Open Project Properties, __add__ this path to Configuration Properties -> VC++ Directories -> Library Directories. Unfortunately, I could not find doc to associate classes with libs; at least not easily so I added them all (please don't do that). –  Jul 08 '21 at 13:11
  • Ok, thank you so much, I forgot this last reference, I finally build the solution without error!! However for completeness I set the target platform to **x64**, because with "Any CPU" (Win32) doesn't work for me, there was conflict with the libs. Thank you again! – Giovanni Jul 08 '21 at 16:26
  • No problem. All the best. –  Jul 08 '21 at 16:27