0032013: Samples - iOS sample compilation errors
[occt.git] / samples / ios / UIKitSample / UIKitSample / OcctViewer.mm
CommitLineData
a9bdd54d 1// Copyright (c) 2017 OPEN CASCADE SAS
2//
b2a63f9b 3// This file is part of the examples of the Open CASCADE Technology software library.
a9bdd54d 4//
b2a63f9b 5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
a9bdd54d 11//
b2a63f9b 12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
a9bdd54d 21
22#include "OcctViewer.h"
23#include "OcctDocument.h"
24
b2a63f9b 25#include <OpenGl_GraphicDriver.hxx>
26
a9bdd54d 27#include <AIS_ConnectedInteractive.hxx>
a9bdd54d 28#include <Aspect_DisplayConnection.hxx>
29#include <BRep_Builder.hxx>
30#include <BRepMesh_IncrementalMesh.hxx>
31#include <BRepTools.hxx>
32#include <Cocoa_Window.hxx>
33#include <Message.hxx>
34#include <Message_Messenger.hxx>
a9bdd54d 35#include <Prs3d_Drawer.hxx>
b2a63f9b 36#include <StdPrs_ToolTriangulatedShape.hxx>
a9bdd54d 37#include <STEPControl_Reader.hxx>
38#include <STEPCAFControl_Reader.hxx>
39#include <TDF_Tool.hxx>
40#include <TDF_ChildIterator.hxx>
41#include <Transfer_TransientProcess.hxx>
42#include <XSControl_TransferReader.hxx>
43#include <XCAFDoc_DocumentTool.hxx>
44
45// =======================================================================
46// function : OcctViewer
47// purpose :
48// =======================================================================
49OcctViewer::OcctViewer()
50{
51 myDoc = new OcctDocument();
52}
53
54// =======================================================================
55// function : ~OcctViewer
56// purpose :
57// =======================================================================
58OcctViewer::~OcctViewer()
59{
60 //
61}
62
63// =======================================================================
64// function : release
65// purpose :
66// =======================================================================
67void OcctViewer::release()
68{
69 myContext.Nullify();
70 if (!myView.IsNull())
71 {
72 myView->Remove();
73 }
74 myView.Nullify();
75 myViewer.Nullify();
76
77 myDoc.Nullify();
78}
79
80// =======================================================================
81// function : InitViewer
82// purpose :
83// =======================================================================
84bool OcctViewer::InitViewer (UIView* theWin)
85{
86 EAGLContext* aRendCtx = [EAGLContext currentContext];
87 if (theWin == NULL || aRendCtx == NULL)
88 {
89 NSLog(@"Error: No active EAGL context!");
90 release();
91 return false;
92 }
a9bdd54d 93 if (!myView.IsNull())
94 {
95 myView->MustBeResized();
96 myView->Invalidate();
b2a63f9b 97 return true;
a9bdd54d 98 }
b2a63f9b 99
100 Handle(Aspect_DisplayConnection) aDisplayConnection = new Aspect_DisplayConnection();
101 Handle(Graphic3d_GraphicDriver) aGraphicDriver = new OpenGl_GraphicDriver (aDisplayConnection);
102
103 // Create Viewer
104 myViewer = new V3d_Viewer (aGraphicDriver);
105 myViewer->SetDefaultLights();
106 myViewer->SetLightOn();
107
108 // Create AIS context
109 myContext = new AIS_InteractiveContext (myViewer);
110 myContext->SetDisplayMode ((int )AIS_DisplayMode::AIS_Shaded, false);
111
112 myView = myViewer->CreateView();
113 myView->TriedronDisplay (Aspect_TOTP_LEFT_LOWER, Quantity_NOC_WHITE, 0.20, V3d_ZBUFFER);
114
115 Handle(Cocoa_Window) aCocoaWindow = new Cocoa_Window (theWin);
116 myView->SetWindow (aCocoaWindow, aRendCtx);
117 if (!aCocoaWindow->IsMapped())
a9bdd54d 118 {
b2a63f9b 119 aCocoaWindow->Map();
a9bdd54d 120 }
b2a63f9b 121
122 myView->Redraw();
123 myView->MustBeResized();
a9bdd54d 124 return true;
125}
126
127// =======================================================================
128// function : FitAll
129// purpose :
130// =======================================================================
131void OcctViewer::FitAll()
132{
133 if (!myView.IsNull())
134 {
135 myView->FitAll();
136 myView->ZFitAll();
137 }
138}
139
140// =======================================================================
141// function : StartRotation
142// purpose :
143// =======================================================================
144void OcctViewer::StartRotation(int theX, int theY)
145{
146 if (!myView.IsNull())
147 {
148 myView->StartRotation(theX, theY);
149 }
150}
151
152// =======================================================================
153// function : Rotation
154// purpose :
155// =======================================================================
156void OcctViewer::Rotation(int theX, int theY)
157{
158 if (!myView.IsNull())
159 {
160 myView->Rotation(theX, theY);
161 }
162}
163
164// =======================================================================
165// function : Pan
166// purpose :
167// =======================================================================
168void OcctViewer::Pan(int theX, int theY)
169{
170 if (!myView.IsNull())
171 {
172 myView->Pan(theX, theY, 1, Standard_False);
173 }
174}
175
176// =======================================================================
177// function : Zoom
178// purpose :
179// =======================================================================
180void OcctViewer::Zoom(int theX, int theY, double theDelta)
181{
182 if (!myView.IsNull())
183 {
184 if (theX >=0 && theY >=0)
185 {
186 myView->StartZoomAtPoint(theX, theY);
187 myView->ZoomAtPoint(0, 0, (int) theDelta, (int) theDelta);
188 }
189 else
190 {
191 double aCoeff = Abs(theDelta) / 100.0 + 1.0;
192 aCoeff = theDelta > 0.0 ? aCoeff : 1.0 / aCoeff;
193 myView->SetZoom(aCoeff, Standard_True);
194 }
195 }
196}
197
198// =======================================================================
199// function : Select
200// purpose :
201// =======================================================================
202void OcctViewer::Select(int theX, int theY)
203{
204 if (!myContext.IsNull())
205 {
206 myContext->ClearSelected(Standard_False);
207 myContext->MoveTo(theX, theY, myView, Standard_False);
208 myContext->Select(Standard_False);
209 }
210}
211
212// =======================================================================
213// function : ImportSTEP
214// purpose :
215// =======================================================================
216bool OcctViewer::ImportSTEP(std::string theFilename)
217{
218 // create a new document
219 myDoc->InitDoc();
b2a63f9b 220
a9bdd54d 221 STEPCAFControl_Reader aReader;
222 Handle(XSControl_WorkSession) aSession = aReader.Reader().WS();
b2a63f9b 223
224 try
225 {
a9bdd54d 226 if (!aReader.ReadFile (theFilename.c_str()))
227 {
228 clearSession (aSession);
229 return false;
230 }
231
232 if (!aReader.Transfer (myDoc->ChangeDocument()))
233 {
234 clearSession (aSession);
235 return false;
236 }
b2a63f9b 237
a9bdd54d 238 clearSession(aSession);
b2a63f9b 239 }
240 catch (const Standard_Failure& theFailure)
241 {
242 Message::SendFail (TCollection_AsciiString ("Exception raised during STEP import\n[")
243 + theFailure.GetMessageString() + "]\n" + theFilename.c_str());
a9bdd54d 244 return false;
245 }
b2a63f9b 246
a9bdd54d 247 Handle(XCAFDoc_ShapeTool) aShapeTool = XCAFDoc_DocumentTool::ShapeTool (myDoc->Document()->Main());
248 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (myDoc->Document()->Main());
b2a63f9b 249
a9bdd54d 250 TDF_LabelSequence aLabels;
251 aShapeTool->GetFreeShapes (aLabels);
b2a63f9b 252
a9bdd54d 253 // perform meshing explicitly
254 TopoDS_Compound aCompound;
255 BRep_Builder aBuildTool;
256 aBuildTool.MakeCompound (aCompound);
257 for (Standard_Integer aLabIter = 1; aLabIter <= aLabels.Length(); ++aLabIter)
258 {
259 TopoDS_Shape aShape;
260 const TDF_Label& aLabel = aLabels.Value (aLabIter);
261 if (XCAFDoc_ShapeTool::GetShape (aLabel, aShape))
262 {
263 aBuildTool.Add (aCompound, aShape);
264 }
265 }
b2a63f9b 266
a9bdd54d 267 Handle(Prs3d_Drawer) aDrawer = myContext->DefaultDrawer();
7f24b768 268 Standard_Real aDeflection = StdPrs_ToolTriangulatedShape::GetDeflection (aCompound, aDrawer);
a9bdd54d 269 if (!BRepTools::Triangulation (aCompound, aDeflection))
270 {
271 BRepMesh_IncrementalMesh anAlgo;
272 anAlgo.ChangeParameters().Deflection = aDeflection;
67441d0c 273 anAlgo.ChangeParameters().Angle = aDrawer->DeviationAngle();
a9bdd54d 274 anAlgo.ChangeParameters().InParallel = Standard_True;
b2a63f9b 275 anAlgo.SetShape (aCompound);
a9bdd54d 276 anAlgo.Perform();
277 }
b2a63f9b 278
a9bdd54d 279 // clear presentations
280 clearContext();
b2a63f9b 281
a9bdd54d 282 // create presentations
283 MapOfPrsForShapes aMapOfShapes;
284 XCAFPrs_Style aDefStyle;
285 aDefStyle.SetColorSurf (Quantity_NOC_GRAY65);
286 aDefStyle.SetColorCurv (Quantity_NOC_GRAY65);
287 for (Standard_Integer aLabIter = 1; aLabIter <= aLabels.Length(); ++aLabIter)
288 {
289 const TDF_Label& aLabel = aLabels.Value (aLabIter);
290 displayWithChildren (*aShapeTool, *aColorTool, aLabel, TopLoc_Location(), aDefStyle, "", aMapOfShapes);
291 }
292
293 return true;
294}
295
296// =======================================================================
297// function : displayWithChildren
298// purpose :
299// =======================================================================
300void OcctViewer::displayWithChildren (XCAFDoc_ShapeTool& theShapeTool,
301 XCAFDoc_ColorTool& theColorTool,
302 const TDF_Label& theLabel,
303 const TopLoc_Location& theParentTrsf,
304 const XCAFPrs_Style& theParentStyle,
305 const TCollection_AsciiString& theParentId,
306 MapOfPrsForShapes& theMapOfShapes)
307{
308 TDF_Label aRefLabel = theLabel;
309 if (theShapeTool.IsReference (theLabel))
310 {
311 theShapeTool.GetReferredShape (theLabel, aRefLabel);
312 }
b2a63f9b 313
a9bdd54d 314 TCollection_AsciiString anEntry;
315 TDF_Tool::Entry (theLabel, anEntry);
316 if (!theParentId.IsEmpty())
317 {
318 anEntry = theParentId + "\n" + anEntry;
319 }
320 anEntry += ".";
b2a63f9b 321
a9bdd54d 322 if (!theShapeTool.IsAssembly (aRefLabel))
323 {
324 Handle(AIS_InteractiveObject) anAis;
325 if (!theMapOfShapes.Find (aRefLabel, anAis))
326 {
a966542b 327 anAis = new CafShapePrs (aRefLabel, theParentStyle, Graphic3d_NameOfMaterial_ShinyPlastified);
a9bdd54d 328 theMapOfShapes.Bind (aRefLabel, anAis);
329 }
b2a63f9b 330
331 Handle(TCollection_HAsciiString) anId = new TCollection_HAsciiString (anEntry);
a9bdd54d 332 Handle(AIS_ConnectedInteractive) aConnected = new AIS_ConnectedInteractive();
333 aConnected->Connect (anAis, theParentTrsf.Transformation());
334 aConnected->SetOwner (anId);
335 aConnected->SetLocalTransformation (theParentTrsf.Transformation());
336 aConnected->SetHilightMode(1);
337 myContext->Display (aConnected, Standard_False);
338 return;
339 }
b2a63f9b 340
a9bdd54d 341 XCAFPrs_Style aDefStyle = theParentStyle;
342 Quantity_Color aColor;
343 if (theColorTool.GetColor (aRefLabel, XCAFDoc_ColorGen, aColor))
344 {
345 aDefStyle.SetColorCurv (aColor);
346 aDefStyle.SetColorSurf (aColor);
347 }
348 if (theColorTool.GetColor (aRefLabel, XCAFDoc_ColorSurf, aColor))
349 {
350 aDefStyle.SetColorSurf (aColor);
351 }
352 if (theColorTool.GetColor (aRefLabel, XCAFDoc_ColorCurv, aColor))
353 {
354 aDefStyle.SetColorCurv (aColor);
355 }
b2a63f9b 356
a9bdd54d 357 for (TDF_ChildIterator childIter (aRefLabel); childIter.More(); childIter.Next())
358 {
359 TDF_Label aLabel = childIter.Value();
360 if (!aLabel.IsNull()
361 && (aLabel.HasAttribute() || aLabel.HasChild()))
362 {
363 TopLoc_Location aTrsf = theParentTrsf * theShapeTool.GetLocation (aLabel);
364 displayWithChildren (theShapeTool, theColorTool, aLabel, aTrsf, aDefStyle, anEntry, theMapOfShapes);
365 }
366 }
367}
368
369// =======================================================================
370// function : clearSession
371// purpose :
372// =======================================================================
373void OcctViewer::clearSession (const Handle(XSControl_WorkSession)& theSession)
374{
375 if (theSession.IsNull())
376 {
377 return;
378 }
b2a63f9b 379
a9bdd54d 380 Handle(Transfer_TransientProcess) aMapReader = theSession->TransferReader()->TransientProcess();
381 if (!aMapReader.IsNull())
382 {
383 aMapReader->Clear();
384 }
b2a63f9b 385
a9bdd54d 386 Handle(XSControl_TransferReader) aTransferReader = theSession->TransferReader();
387 if (!aTransferReader.IsNull())
388 {
389 aTransferReader->Clear(1);
390 }
391}
392
393// =======================================================================
394// function : clearContext
395// purpose :
396// =======================================================================
b2a63f9b 397void OcctViewer::clearContext()
a9bdd54d 398{
399 if (!myContext.IsNull())
400 {
401 myContext->ClearSelected(Standard_False);
402 myContext->RemoveAll(Standard_False);
403 }
404}