diff --git a/Cassiopee/OCC/OCC/Atomic/createEmptyCAD.cpp b/Cassiopee/OCC/OCC/Atomic/createEmptyCAD.cpp index cbbf8cad3..fa069046f 100644 --- a/Cassiopee/OCC/OCC/Atomic/createEmptyCAD.cpp +++ b/Cassiopee/OCC/OCC/Atomic/createEmptyCAD.cpp @@ -24,6 +24,21 @@ #include "TopExp.hxx" #include "TopExp_Explorer.hxx" +#include "TDocStd_Document.hxx" +#include "XCAFApp_Application.hxx" +#include "TColStd_SequenceOfAsciiString.hxx" + +/* +#include +#include +#include +#include +#include +#include +#include +#include +*/ + // ============================================================================ /* Convert CAD to OpenCascade hook */ // ============================================================================ @@ -51,6 +66,13 @@ PyObject* K_OCC::createEmptyCAD(PyObject* self, PyObject* args) char* fileFmtC = new char [l+1]; strcpy(fileFmtC, fileFmt); + // Document + Handle(XCAFApp_Application) app = XCAFApp_Application::GetApplication(); // init app at first call + TDocStd_Document* doc = NULL; + //doc = new TDocStd_Document("MDTV-Standard"); + doc = new TDocStd_Document("XmlXCAF"); + app->InitDocument(doc); + // capsule PyObject* hook; E_Int sizePacket = 6; @@ -60,7 +82,7 @@ PyObject* K_OCC::createEmptyCAD(PyObject* self, PyObject* args) packet[2] = edges; // the edge map packet[3] = fileNameC; // CAD file name packet[4] = fileFmtC; // CAD file format - packet[5] = NULL; // document + packet[5] = doc; // document #if (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 7) || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 1) hook = PyCObject_FromVoidPtr(packet, NULL); diff --git a/Cassiopee/OCC/OCC/Atomic/freeHook.cpp b/Cassiopee/OCC/OCC/Atomic/freeHook.cpp index c1b1c9d8e..612b80855 100644 --- a/Cassiopee/OCC/OCC/Atomic/freeHook.cpp +++ b/Cassiopee/OCC/OCC/Atomic/freeHook.cpp @@ -52,7 +52,7 @@ PyObject* K_OCC::freeHook(PyObject* self, PyObject* args) TopoDS_Shape* shp = (TopoDS_Shape*)packet[0]; char* fileName = (char*)packet[3]; char* fileFmt = (char*)packet[4]; - TDocStd_Document* doc = (TDocStd_Document*)packet[5]; + //TDocStd_Document* doc = (TDocStd_Document*)packet[5]; delete shp; delete edges; diff --git a/Cassiopee/OCC/OCC/Atomic/getFaceNameInOCAF.cpp b/Cassiopee/OCC/OCC/Atomic/getFaceNameInOCAF.cpp new file mode 100644 index 000000000..d240ae558 --- /dev/null +++ b/Cassiopee/OCC/OCC/Atomic/getFaceNameInOCAF.cpp @@ -0,0 +1,159 @@ +/* + Copyright 2013-2025 Onera. + + This file is part of Cassiopee. + + Cassiopee is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Cassiopee is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Cassiopee. If not, see . +*/ +#include "occ.h" + +#include "TDF_Label.hxx" +#include "TDF_LabelSequence.hxx" +#include "TDF_Tool.hxx" +#include "XCAFDoc_ShapeTool.hxx" +#include "XCAFDoc_DocumentTool.hxx" +#include "TDocStd_Document.hxx" +#include "TDataStd_Name.hxx" +#include "TDF_ChildIterator.hxx" +#include "TopExp.hxx" +#include "TopExp_Explorer.hxx" +#include "TopTools_IndexedMapOfShape.hxx" +#include "TopoDS.hxx" +#include "TopoDS_Shape.hxx" +#include "TopoDS_Face.hxx" +#include "Geom_Surface.hxx" +#include "BRep_Tool.hxx" + +//===================================================================== +// Get face names +// Return [names, [face no]] +//===================================================================== +PyObject* K_OCC::getFaceNameInOCAF(PyObject* self, PyObject* args) +{ + PyObject* hook; + if (!PYPARSETUPLE_(args, O_, &hook)) return NULL; + + void** packet = NULL; +#if (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 7) || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 1) + packet = (void**) PyCObject_AsVoidPtr(hook); +#else + packet = (void**) PyCapsule_GetPointer(hook, NULL); +#endif + + TDocStd_Document* doc = (TDocStd_Document*)packet[5]; + if (doc == NULL) + { + PyErr_SetString(PyExc_TypeError, "printOCAF: no OCAF document."); + return NULL; + } + + TopoDS_Shape* topShape = (TopoDS_Shape*)packet[0]; + + TopTools_IndexedMapOfShape& allFaces = *(TopTools_IndexedMapOfShape*)packet[1]; + + TDF_LabelSequence labels; + Handle(XCAFDoc_ShapeTool) shapeTool = XCAFDoc_DocumentTool::ShapeTool(doc->Main()); + shapeTool->GetShapes(labels); + + PyObject* out = NULL; + out = PyList_New(0); + + for (Standard_Integer i = 1; i <= labels.Length(); i++) + { + TDF_Label label = labels.Value(i); + Handle(TDataStd_Name) NAME = new TDataStd_Name(); + // Get shape associated with label + TopoDS_Shape shape = shapeTool->GetShape(label); + + // Face list corresponding to that shape + /* + TopTools_IndexedMapOfShape faces = TopTools_IndexedMapOfShape(); + TopExp::MapShapes(shape, TopAbs_FACE, faces); + for (E_Int k = 1; k <= allFaces.Extent(); k++) // top shape + { + TopoDS_Face F1 = TopoDS::Face(allFaces(k)); + auto itr1 = allFaces(k); + for (E_Int j = 1; j <= faces.Extent(); j++) + { + //printf("compound faces = %d\n", j); + TopoDS_Face F2 = TopoDS::Face(faces(j)); + auto itr2 = faces(j); + if (F1.IsEqual(F2) == true) printf("id detected=%d %d \n", j, k); + if (itr1 == itr2) printf("id detected=%d %d\n", j, k); + } + } + */ + + /* + TopExp_Explorer explorer2(shape, TopAbs_FACE); + while (explorer2.More()) + { + TopoDS_Face face1 = TopoDS::Face(explorer2.Current()); + + TopExp_Explorer explorer(*topShape, TopAbs_FACE); + while (explorer.More()) + { + TopoDS_Face face2 = TopoDS::Face(explorer.Current()); + if (explorer.Current() == explorer2.Current()) printf("found\n"); + if (face1.IsEqual(face2)) printf("found\n"); + explorer.Next(); + } + explorer2.Next(); + } + */ + + if (label.FindAttribute(TDataStd_Name::GetID(), NAME)) // retourne tous les attributs de type string + { + TopExp_Explorer explorer2(shape, TopAbs_FACE); + E_Int n = 0; + while (explorer2.More()) + { + TopoDS_Face face1 = TopoDS::Face(explorer2.Current()); + TopExp_Explorer explorer(*topShape, TopAbs_FACE); + E_Int n1 = 0; + while (explorer.More()) // topshape + { + TopoDS_Face face2 = TopoDS::Face(explorer.Current()); + if (explorer.Current() == explorer2.Current()) printf("found by ptr\n"); + if (face1.IsEqual(face2)) printf("found face\n"); + if (face1 == face2) printf("found face\n"); + if (face1.IsSame(face2)) printf("found face\n"); + Handle(TopoDS_TShape) tface1 = face1.TShape(); + Handle(TopoDS_TShape) tface2 = face2.TShape(); + if (tface1 == tface2) printf("found tface\n"); + + + Handle(Geom_Surface) surface1 = BRep_Tool::Surface(face1); + Handle(Geom_Surface) surface2 = BRep_Tool::Surface(face2); + //if (surface1->IsEqual(surface2)) printf("found geom\n"); + + explorer.Next(); + n1++; + } + n++; + explorer2.Next(); + } + + TCollection_ExtendedString labelName = NAME->Get(); + TCollection_AsciiString asciiStr(labelName); + const char* name = asciiStr.ToCString(); // component name + + printf("has string attribute %s = %d\n", name, n); + + PyObject* pystring = PyUnicode_FromString(name); + PyList_Append(out, pystring); Py_DECREF(pystring); + } + } + return out; +} \ No newline at end of file diff --git a/Cassiopee/OCC/OCC/Atomic/printOCAF.cpp b/Cassiopee/OCC/OCC/Atomic/printOCAF.cpp index 9a8965a63..8023be2f8 100644 --- a/Cassiopee/OCC/OCC/Atomic/printOCAF.cpp +++ b/Cassiopee/OCC/OCC/Atomic/printOCAF.cpp @@ -18,7 +18,6 @@ */ #include "occ.h" -#include "STEPCAFControl_Reader.hxx" #include "TDF_Label.hxx" #include "TDF_LabelSequence.hxx" #include "TDF_Tool.hxx" @@ -26,9 +25,26 @@ #include "XCAFDoc_DocumentTool.hxx" #include "TDocStd_Document.hxx" #include "TDataStd_Name.hxx" +#include "TDataStd_AsciiString.hxx" #include "TDF_ChildIterator.hxx" #include +#include "TNaming_UsedShapes.hxx" +#include "TNaming_NamedShape.hxx" +#include "TNaming_Evolution.hxx" +#include "TDF_Reference.hxx" +#include "TDataStd_Expression.hxx" +#include "TDataStd_Comment.hxx" +#include "TDataStd_ExtStringArray.hxx" +#include "TDataStd_ExtStringList.hxx" +#include "TDataStd_Integer.hxx" +#include "TDataStd_IntegerArray.hxx" +#include "TDataStd_IntegerList.hxx" +#include "TDataStd_Real.hxx" +#include "TDataStd_RealArray.hxx" +#include "TDataStd_RealList.hxx" + + // recursive iteration through labels void iterateLabels(const TDF_Label& label) { @@ -37,12 +53,119 @@ void iterateLabels(const TDF_Label& label) TDF_Tool::Entry(label, es); // retourne l'entry de la label (3:0:3 indiquant sa position dans la hierarchie) std::cout << "Info: Label entry: " << es.ToCString() << std::endl; - // Print the label string attributes + // Number of attributes + E_Int n = TDF_Tool::NbAttributes(label); + std::cout << "Info: number of attached attributes: " << n << std::endl; + + // Name String attributes Handle(TDataStd_Name) nameAttr; - if (label.FindAttribute(TDataStd_Name::GetID(), nameAttr)) + if (label.FindAttribute(TDataStd_Name::GetID(), nameAttr)) { TCollection_ExtendedString name = nameAttr->Get(); - std::cout << "Info: Label string: " << name << std::endl; + std::cout << ">>>>: Label string: " << name << std::endl; + } + + // NamedShape attribute + Handle(TNaming_NamedShape) tnnsAttr; + if (label.FindAttribute(TNaming_NamedShape::GetID(), tnnsAttr)) + { + std::cout << ">>>>: TNaming_NamedShape " << std::endl; + std::cout << ">>>>: TNaming_NamedShape Version " << tnnsAttr->Version() << std::endl; + TNaming_Evolution evo = tnnsAttr->Evolution(); + if (evo == TNaming_PRIMITIVE) + std::cout << ">>>>: Evolution PRIMITIVE"<< std::endl; + + } + + // UsedShapes attribute + Handle(TNaming_UsedShapes) tnusAttr; + if (label.FindAttribute(TNaming_UsedShapes::GetID(), tnusAttr)) + { + std::cout << ">>>>: TNaming_UsedShapes " << std::endl; + } + + // Asciistring attribute + Handle(TDataStd_AsciiString) tasAttr; + if (label.FindAttribute(TDataStd_AsciiString::GetID(), tasAttr)) + { + std::cout << ">>>>: Ascii string " << tasAttr->Get() << std::endl; + } + + // Reference (parametre?) + Handle(TDF_Reference) ref; + if (label.FindAttribute(TDF_Reference::GetID(), ref)) + { + std::cout << ">>>>: reference detected " << std::endl; + } + + // Comment + Handle(TDataStd_Comment) attComment; + if (label.FindAttribute(TDataStd_Comment::GetID(), attComment)) + { + std::cout << ">>>>: comment detected " << std::endl; + } + + // Expression + Handle(TDataStd_Expression) attExpression; + if (label.FindAttribute(TDataStd_Expression::GetID(), attExpression)) + { + std::cout << ">>>>: expression detected " << std::endl; + } + + // ExtStringArray + Handle(TDataStd_ExtStringArray) attExtStringArray; + if (label.FindAttribute(TDataStd_Expression::GetID(), attExtStringArray)) + { + std::cout << ">>>>: extStringArray detected " << endl; + } + + // ExtStringList + Handle(TDataStd_ExtStringList) attExtStringList; + if (label.FindAttribute(TDataStd_Expression::GetID(), attExtStringList)) + { + std::cout << ">>>>: extStringList detected " << endl; + } + + // Integer + Handle(TDataStd_Integer) attInteger; + if (label.FindAttribute(TDataStd_Integer::GetID(), attInteger)) + { + std::cout << ">>>>: integer detected " << endl; + } + + // IntegerArray + Handle(TDataStd_IntegerArray) attIntegerArray; + if (label.FindAttribute(TDataStd_IntegerArray::GetID(), attIntegerArray)) + { + std::cout << ">>>>: integerArray detected " << endl; + } + + // IntegerList + Handle(TDataStd_IntegerList) attIntegerList; + if (label.FindAttribute(TDataStd_IntegerArray::GetID(), attIntegerList)) + { + std::cout << ">>>>: integerList detected " << endl; + } + + // Real + Handle(TDataStd_Real) attReal; + if (label.FindAttribute(TDataStd_Real::GetID(), attReal)) + { + std::cout << ">>>>: real detected " << endl; + } + + // RealArray + Handle(TDataStd_RealArray) attRealArray; + if (label.FindAttribute(TDataStd_RealArray::GetID(), attRealArray)) + { + std::cout << ">>>>: realArray detected " << endl; + } + + // RealList + Handle(TDataStd_RealList) attRealList; + if (label.FindAttribute(TDataStd_RealArray::GetID(), attRealList)) + { + std::cout << ">>>>: realList detected " << endl; } // Iterate through child labels @@ -108,7 +231,7 @@ PyObject* K_OCC::printShapeOCAF(PyObject* self, PyObject* args) //============================================================ TDF_LabelSequence labels; Handle(XCAFDoc_ShapeTool) shapeTool = XCAFDoc_DocumentTool::ShapeTool(doc->Main()); - shapeTool->GetFreeShapes(labels); + shapeTool->GetShapes(labels); for (Standard_Integer i = 1; i <= labels.Length(); i++) { @@ -121,18 +244,13 @@ PyObject* K_OCC::printShapeOCAF(PyObject* self, PyObject* args) Handle(TDataStd_Name) NAME = new TDataStd_Name(); if (label.FindAttribute(TDataStd_Name::GetID(), NAME)) // retourne tous les attributs de type string { - printf("has string attribute\n"); TCollection_ExtendedString labelName = NAME->Get(); std::cout << "Info: Label name: " << labelName << std::endl; } else - { printf("no string attribute\n"); } + { /*printf("no string attribute\n");*/ } } Py_INCREF(Py_None); return Py_None; } - - - - diff --git a/Cassiopee/OCC/OCC/Atomic/readCAD.cpp b/Cassiopee/OCC/OCC/Atomic/readCAD.cpp index a4a3fdd39..e5bbc2444 100644 --- a/Cassiopee/OCC/OCC/Atomic/readCAD.cpp +++ b/Cassiopee/OCC/OCC/Atomic/readCAD.cpp @@ -18,6 +18,7 @@ */ #include "occ.h" + // IGES/STEP #include "IGESControl_Reader.hxx" #include "STEPControl_Reader.hxx" @@ -33,6 +34,10 @@ #include "STEPCAFControl_Reader.hxx" #include "XCAFDoc_DocumentTool.hxx" #include "TDocStd_Document.hxx" +#include "XCAFApp_Application.hxx" + +#include "XmlDrivers.hxx" +#include "XmlXCAFDrivers.hxx" // ============================================================================ /* Convert CAD to OpenCascade hook */ @@ -67,11 +72,14 @@ PyObject* K_OCC::readCAD(PyObject* self, PyObject* args) *shp = reader.OneShape(); // Read document + Handle(XCAFApp_Application) app = XCAFApp_Application::GetApplication(); // init app at first call IGESCAFControl_Reader reader2; reader2.ReadFile(fileName); - doc = new TDocStd_Document("MDTV-Standard"); + //doc = new TDocStd_Document("MDTV-Standard"); + doc = new TDocStd_Document("XmlXCAF"); Handle(TDocStd_Document) doc2 = doc; reader2.Transfer(doc2); + app->InitDocument(doc2); } else if (strcmp(fileFmt, "fmt_step") == 0) { @@ -82,11 +90,17 @@ PyObject* K_OCC::readCAD(PyObject* self, PyObject* args) *shp = reader.OneShape(); // Read document + Handle(XCAFApp_Application) app = XCAFApp_Application::GetApplication(); // init app at first call STEPCAFControl_Reader reader2; reader2.ReadFile(fileName); - doc = new TDocStd_Document("MDTV-Standard"); + //doc = new TDocStd_Document("MDTV-Standard"); + doc = new TDocStd_Document("XmlXCAF"); Handle(TDocStd_Document) doc2 = doc; - reader2.Transfer(doc2); + reader2.Transfer(doc2); + app->InitDocument(doc2); + //XmlXCAFDrivers::DefineFormat(*app); // register drivers + PCDM_StoreStatus status = app->SaveAs(doc2, "toto.xml"); + if (status != PCDM_SS_OK) printf("can not write document\n"); } // Extract surfaces diff --git a/Cassiopee/OCC/OCC/occ.cpp b/Cassiopee/OCC/OCC/occ.cpp index 6ca4585c9..532961106 100644 --- a/Cassiopee/OCC/OCC/occ.cpp +++ b/Cassiopee/OCC/OCC/occ.cpp @@ -36,6 +36,7 @@ static PyMethodDef Pyocc [] = {"printOCAF", K_OCC::printOCAF, METH_VARARGS}, {"printShapeOCAF", K_OCC::printShapeOCAF, METH_VARARGS}, + {"getFaceNameInOCAF", K_OCC::getFaceNameInOCAF, METH_VARARGS}, {"bottle", K_OCC::bottle, METH_VARARGS}, {"addSphere", K_OCC::addSphere, METH_VARARGS}, diff --git a/Cassiopee/OCC/OCC/occ.h b/Cassiopee/OCC/OCC/occ.h index ed63bd3b0..ef41b9e68 100644 --- a/Cassiopee/OCC/OCC/occ.h +++ b/Cassiopee/OCC/OCC/occ.h @@ -35,6 +35,7 @@ namespace K_OCC PyObject* printOCAF(PyObject* self, PyObject* args); PyObject* printShapeOCAF(PyObject* self, PyObject* args); + PyObject* getFaceNameInOCAF(PyObject* self, PyObject* args); PyObject* bottle(PyObject* self, PyObject* args); PyObject* addSphere(PyObject* self, PyObject* args); diff --git a/Cassiopee/OCC/srcs.py b/Cassiopee/OCC/srcs.py index bf64c3627..97bbad026 100644 --- a/Cassiopee/OCC/srcs.py +++ b/Cassiopee/OCC/srcs.py @@ -40,6 +40,7 @@ def getFiles(module): 'OCC/Atomic/freeHook.cpp', 'OCC/Atomic/printOCAF.cpp', + 'OCC/Atomic/getFaceNameInOCAF.cpp', 'OCC/Atomic/bottle.cpp', 'OCC/Atomic/addSphere.cpp',