Salome HOME
[bos #42109][CEA] (2024) Import med and show python console shortcuts does not work...
[modules/gui.git] / src / SPV3D / SPV3D_Prs.cxx
1 // Copyright (C) 2023-2024  CEA, EDF
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 //  SALOME PV3DViewer : build PV3D viewer into Salome desktop
21 //  File   : SPV3D_Prs.cxx
22
23 #include "SPV3D_Prs.h"
24 #include "SPV3D_ViewWindow.h"
25
26 #include <vtkAppendPolyData.h>
27 #include "vtkActor.h"
28 #include "vtkPolyData.h"
29 #include "vtkCellData.h"
30 #include "vtkMapper.h"
31 #include "pqServer.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>
39
40 #include <QDebug>
41
42 vtkIdType SPV3D_Prs::cellid = 0;
43 vtkIdType SPV3D_Prs::nbsolid = 0;
44
45 SPV3D_EXPORTSPV3DData::SPV3D_EXPORTSPV3DData()
46 {
47   nbsolid = 0;
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);
52 }
53 void SPV3D_EXPORTSPV3DData::SetPolyData(vtkPolyData* ds)
54 {
55   _multiGEOMData->SetBlock(nbsolid , ds);
56   nbsolid++;
57 }
58
59 void SPV3D_EXPORTSPV3DData::SetPrs(vtkPolyData* ds, const char* entry)
60 {
61   unsigned int id;
62   if (havePrs(entry, id))
63     return;
64   auto nbCells( ds->GetNumberOfCells() );
65
66   vtkNew<vtkIdTypeArray> solidIdArray;
67   qInfo() << "nb of cells: " << nbCells;
68   solidIdArray->SetNumberOfComponents(1);
69   solidIdArray->SetNumberOfTuples( nbCells );
70   solidIdArray->SetName("Solid id");
71
72 //  vtkNew<vtkIdTypeArray> cellIdArray;
73 //  cellIdArray->SetNumberOfComponents(1);
74 //  cellIdArray->SetNumberOfTuples( nbCells );
75 //  cellIdArray->SetName("Edge id");
76
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++; });
82
83   ds->GetCellData()->AddArray( solidIdArray );
84 // ->GetCellData()->AddArray( cellIdArray );
85   SetPolyData(ds);
86   updateSource_for_display();
87 }
88
89 void SPV3D_EXPORTSPV3DData::RemovePrs(const char* entry)
90 {
91   unsigned int id;
92   if (!havePrs(entry, id))
93   {
94     qWarning() << "Can not find solid with entry: " << entry;
95     return;
96   }
97   _multiGEOMData->RemoveBlock(id);
98   updateSource_for_display();
99 }
100
101 void SPV3D_EXPORTSPV3DData::updateSource_for_display()
102 {
103   vtkNew<vtkAppendPolyData> appendFilter;
104   nbsolid = _multiGEOMData->GetNumberOfBlocks();
105   qInfo() <<"number of block: " << nbsolid;
106   for (unsigned int i = 0; i < nbsolid; ++i)
107   {
108       vtkPolyData* polyData = vtkPolyData::SafeDownCast(_multiGEOMData->GetBlock(i));
109       if (polyData)
110       {
111         vtkIdTypeArray* retrievedArray = vtkIdTypeArray::SafeDownCast(polyData->GetCellData()->GetArray("Solid id"));
112         if (retrievedArray)
113           appendFilter->AddInputData(polyData);
114       }
115   }
116   appendFilter->Update();
117   vtkNew<vtkPolyData> ds;
118   ds->ShallowCopy(appendFilter->GetOutput());
119
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();
126
127   //pqServerManagerModel* smmodel = pqApplicationCore::instance()->getServerManagerModel();
128   //pqProxy *producerBase2( smmodel->findItem<pqProxy*>(producerBase) );
129   //if(producerBase2 && !_name.empty())
130   //  producerBase2->rename( _name.c_str() );
131 }
132
133 bool SPV3D_EXPORTSPV3DData::IsVisible() const
134 {
135   if( IsNull() )
136     return false;
137   return GetRepresentation()->isVisible();
138 }
139
140 bool SPV3D_EXPORTSPV3DData::havePrs(const char* entry, unsigned int & id)
141 {
142   vtkIdType solid_id = SPV3D_Prs::FromEntryToVtkId(entry);
143   id = 0;
144   while (id < _multiGEOMData->GetNumberOfBlocks())
145   {
146       vtkPolyData* polyData = vtkPolyData::SafeDownCast(_multiGEOMData->GetBlock(id));
147       if (polyData)
148       {
149         vtkIdTypeArray* retrievedArray = vtkIdTypeArray::SafeDownCast(polyData->GetCellData()->GetArray("Solid id"));
150         if (retrievedArray && retrievedArray->GetValue(0) == solid_id)
151           break;
152       }
153     id++;
154   }
155
156   return id < _multiGEOMData->GetNumberOfBlocks();
157 }
158
159 void SPV3D_EXPORTSPV3DData::Hide() const
160 {
161   if( GetRepresentation() )
162     GetRepresentation()->setVisible(0);
163 }
164
165 SPV3D_Prs::SPV3D_Prs( const char* entry , SPV3D_ViewWindow *view) : SALOME_PV3DPrs(entry),_view(view)
166 {
167 }
168
169 vtkIdType SPV3D_Prs::FromEntryToVtkId (const char* entry)
170 {
171   char *str = new char[strlen(entry)+1];
172   strcpy(str, entry);
173
174   const char delimiter[2] = ":";
175
176   char* token;
177   token = strtok(str, delimiter);
178
179   std::uint32_t ret(atoi(token));
180   token = strtok(NULL, delimiter);
181
182   while (token != NULL) {
183       ret <<=8;
184       ret |= atoi(token);
185       token = strtok(NULL, delimiter);
186   }
187
188   return static_cast<vtkIdType> (ret);
189 }
190
191 const char* SPV3D_Prs::FromVtkIdToEntry(vtkIdType id)
192 {
193   int d = id & 0xFF;
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);
198   return ret.c_str();
199 }
200
201 SPV3D_Prs *SPV3D_Prs::deepCopy() const
202 {
203   SPV3D_Prs *ret = new SPV3D_Prs( *this );
204   return ret;
205 }
206
207 pqProxy *getPQProxy(vtkSMProxy *proxy)
208 {
209   pqServerManagerModel* smmodel = pqApplicationCore::instance()->getServerManagerModel();
210   return smmodel->findItem<pqProxy*>(proxy);
211 }
212
213 SPV3D_Prs:: ~SPV3D_Prs() 
214 {
215 }
216
217 void SPV3D_Prs::DisplayIn( SALOME_View* v ) const
218 {
219   SALOME_PV3DPrs::DisplayIn(v);
220 }
221
222 bool SPV3D_Prs::IsNull() const
223 {
224   if(_pvRendInfo)
225   {
226     unsigned int id;
227     return !_pvRendInfo->havePrs(GetEntry(),id);
228   }
229   return true;
230 }
231
232 void SPV3D_Prs::FillUsingActor(vtkActor *actor) const
233 {
234   std::cout << "Hello FillUsingActor"<<std::endl;
235   int id = _view->isEntryAlreadyExist( GetEntry() );
236
237   if(id == -1) // not exist
238   {
239     actor->GetMapper()->Update();
240     vtkDataObject *ds = actor->GetMapper()->GetInput();
241     vtkPolyData *ds2 = vtkPolyData::SafeDownCast(ds);
242     if(ds2)
243     {
244       _view->ExportToSPV3D(ds2, GetEntry());
245     }
246   }
247   else
248   {
249     qWarning() << "Geometry Object is already in viewer window";
250   }
251 }