1 // Copyright (C) 2023-2024 CEA, EDF
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // SALOME PV3DViewer : build PV3D viewer into Salome desktop
21 // File : SPV3D_Prs.cxx
23 #include "SPV3D_Prs.h"
24 #include "SPV3D_ViewWindow.h"
26 #include <vtkAppendPolyData.h>
28 #include "vtkPolyData.h"
29 #include "vtkCellData.h"
30 #include "vtkMapper.h"
32 #include "pqApplicationCore.h"
33 #include "pqServerManagerModel.h"
34 #include "pqObjectBuilder.h"
35 #include "pqPipelineSource.h"
36 #include "vtkSMSourceProxy.h"
37 #include "vtkPVTrivialProducer.h"
38 #include <pqDataRepresentation.h>
42 vtkIdType SPV3D_Prs::cellid = 0;
43 vtkIdType SPV3D_Prs::nbsolid = 0;
45 SPV3D_EXPORTSPV3DData::SPV3D_EXPORTSPV3DData()
48 _multiGEOMData = vtkSmartPointer<vtkMultiBlockDataSet>::New();
49 pqServer *serv(pqApplicationCore::instance()->getServerManagerModel()->findServer(pqServerResource("builtin:")));
50 pqObjectBuilder *builder(pqApplicationCore::instance()->getObjectBuilder());
51 _sourceProducer = builder->createSource("sources","PVTrivialProducer",serv);
53 void SPV3D_EXPORTSPV3DData::SetPolyData(vtkPolyData* ds)
55 _multiGEOMData->SetBlock(nbsolid , ds);
59 void SPV3D_EXPORTSPV3DData::SetPrs(vtkPolyData* ds, const char* entry)
62 if (havePrs(entry, id))
64 auto nbCells( ds->GetNumberOfCells() );
66 vtkNew<vtkIdTypeArray> solidIdArray;
67 qInfo() << "nb of cells: " << nbCells;
68 solidIdArray->SetNumberOfComponents(1);
69 solidIdArray->SetNumberOfTuples( nbCells );
70 solidIdArray->SetName("Solid id");
72 // vtkNew<vtkIdTypeArray> cellIdArray;
73 // cellIdArray->SetNumberOfComponents(1);
74 // cellIdArray->SetNumberOfTuples( nbCells );
75 // cellIdArray->SetName("Edge id");
77 vtkIdType *pt( solidIdArray->GetPointer(0) );
78 vtkIdType solidid = SPV3D_Prs::FromEntryToVtkId(entry);
79 std::for_each(pt,pt+nbCells,[solidid](vtkIdType& elt) { elt = solidid;});
80 // pt = cellIdArray->GetPointer(0);
81 // std::for_each(pt,pt+nbCells,[](vtkIdType& elt) { elt = SPV3D_Prs::cellid;SPV3D_Prs::cellid++; });
83 ds->GetCellData()->AddArray( solidIdArray );
84 // ->GetCellData()->AddArray( cellIdArray );
86 updateSource_for_display();
89 void SPV3D_EXPORTSPV3DData::RemovePrs(const char* entry)
92 if (!havePrs(entry, id))
94 qWarning() << "Can not find solid with entry: " << entry;
97 _multiGEOMData->RemoveBlock(id);
98 updateSource_for_display();
101 void SPV3D_EXPORTSPV3DData::updateSource_for_display()
103 vtkNew<vtkAppendPolyData> appendFilter;
104 nbsolid = _multiGEOMData->GetNumberOfBlocks();
105 qInfo() <<"number of block: " << nbsolid;
106 for (unsigned int i = 0; i < nbsolid; ++i)
108 vtkPolyData* polyData = vtkPolyData::SafeDownCast(_multiGEOMData->GetBlock(i));
111 vtkIdTypeArray* retrievedArray = vtkIdTypeArray::SafeDownCast(polyData->GetCellData()->GetArray("Solid id"));
113 appendFilter->AddInputData(polyData);
116 appendFilter->Update();
117 vtkNew<vtkPolyData> ds;
118 ds->ShallowCopy(appendFilter->GetOutput());
120 vtkSMProxy* producerBase = _sourceProducer->getProxy();
121 vtkSMSourceProxy *producer(vtkSMSourceProxy::SafeDownCast(producerBase));
122 vtkObjectBase *clientSideObject(producer->GetClientSideObject());
123 vtkPVTrivialProducer *clientSideObjectCast = vtkPVTrivialProducer::SafeDownCast(clientSideObject);
124 clientSideObjectCast->SetOutput(ds);
125 _sourceProducer->updatePipeline();
127 //pqServerManagerModel* smmodel = pqApplicationCore::instance()->getServerManagerModel();
128 //pqProxy *producerBase2( smmodel->findItem<pqProxy*>(producerBase) );
129 //if(producerBase2 && !_name.empty())
130 // producerBase2->rename( _name.c_str() );
133 bool SPV3D_EXPORTSPV3DData::IsVisible() const
137 return GetRepresentation()->isVisible();
140 bool SPV3D_EXPORTSPV3DData::havePrs(const char* entry, unsigned int & id)
142 vtkIdType solid_id = SPV3D_Prs::FromEntryToVtkId(entry);
144 while (id < _multiGEOMData->GetNumberOfBlocks())
146 vtkPolyData* polyData = vtkPolyData::SafeDownCast(_multiGEOMData->GetBlock(id));
149 vtkIdTypeArray* retrievedArray = vtkIdTypeArray::SafeDownCast(polyData->GetCellData()->GetArray("Solid id"));
150 if (retrievedArray && retrievedArray->GetValue(0) == solid_id)
156 return id < _multiGEOMData->GetNumberOfBlocks();
159 void SPV3D_EXPORTSPV3DData::Hide() const
161 if( GetRepresentation() )
162 GetRepresentation()->setVisible(0);
165 SPV3D_Prs::SPV3D_Prs( const char* entry , SPV3D_ViewWindow *view) : SALOME_PV3DPrs(entry),_view(view)
169 vtkIdType SPV3D_Prs::FromEntryToVtkId (const char* entry)
171 char *str = new char[strlen(entry)+1];
174 const char delimiter[2] = ":";
177 token = strtok(str, delimiter);
179 std::uint32_t ret(atoi(token));
180 token = strtok(NULL, delimiter);
182 while (token != NULL) {
185 token = strtok(NULL, delimiter);
188 return static_cast<vtkIdType> (ret);
191 const char* SPV3D_Prs::FromVtkIdToEntry(vtkIdType id)
194 std::uint32_t c_work = ( id & 0xFF00 ); int c = c_work >> 8;
195 std::uint32_t b_work = ( id & 0xFF0000 ); int b = b_work >> 16;
196 std::uint32_t a_work = ( id & 0xFF000000 ); int a = a_work >> 24;
197 std::string ret = std::to_string(a) + ":" + std::to_string(b) + ":" + std::to_string(c)+ ":" + std::to_string(d);
201 SPV3D_Prs *SPV3D_Prs::deepCopy() const
203 SPV3D_Prs *ret = new SPV3D_Prs( *this );
207 pqProxy *getPQProxy(vtkSMProxy *proxy)
209 pqServerManagerModel* smmodel = pqApplicationCore::instance()->getServerManagerModel();
210 return smmodel->findItem<pqProxy*>(proxy);
213 SPV3D_Prs:: ~SPV3D_Prs()
217 void SPV3D_Prs::DisplayIn( SALOME_View* v ) const
219 SALOME_PV3DPrs::DisplayIn(v);
222 bool SPV3D_Prs::IsNull() const
227 return !_pvRendInfo->havePrs(GetEntry(),id);
232 void SPV3D_Prs::FillUsingActor(vtkActor *actor) const
234 std::cout << "Hello FillUsingActor"<<std::endl;
235 int id = _view->isEntryAlreadyExist( GetEntry() );
237 if(id == -1) // not exist
239 actor->GetMapper()->Update();
240 vtkDataObject *ds = actor->GetMapper()->GetInput();
241 vtkPolyData *ds2 = vtkPolyData::SafeDownCast(ds);
244 _view->ExportToSPV3D(ds2, GetEntry());
249 qWarning() << "Geometry Object is already in viewer window";