1 // Created on: 2017-06-16
2 // Created by: Natalia ERMOLAEVA
3 // Copyright (c) 2017 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <inspector/ToolsDraw.hxx>
18 #include <AIS_InteractiveContext.hxx>
19 #include <BRep_Builder.hxx>
20 #include <BRepTools.hxx>
22 #include <DDocStd.hxx>
24 #include <Draw_PluginMacro.hxx>
25 #include <NCollection_DataMap.hxx>
26 #include <Standard_Stream.hxx>
27 #include <TDocStd_Application.hxx>
28 #include <TopoDS_Shape.hxx>
29 #include <ViewerTest.hxx>
30 #include <ViewerTest_DoubleMapOfInteractiveAndName.hxx>
32 #include <inspector/TInspectorAPI_PluginParameters.hxx>
33 #include <inspector/TInspector_Communicator.hxx>
36 extern ViewerTest_DoubleMapOfInteractiveAndName& GetMapOfAIS();
38 Standard_EXPORT ViewerTest_DoubleMapOfInteractiveAndName& GetMapOfAIS();
41 static TInspector_Communicator* MyCommunicator;
43 // =======================================================================
44 // function : Communicator
45 // purpose : defines plugin library name by the command argument
46 // =======================================================================
47 TInspector_Communicator* ToolsDraw::Communicator()
49 return MyCommunicator;
52 // =======================================================================
53 // function : convertToPluginName
54 // purpose : defines plugin library name by the command argument
55 // =======================================================================
56 Standard_Boolean convertToPluginName (const TCollection_AsciiString& theArgument,
57 TCollection_AsciiString& thePluginName)
59 TCollection_AsciiString anArgument = theArgument;
60 anArgument.LowerCase();
62 if (anArgument == "dfbrowser") { thePluginName = "TKDFBrowser"; return Standard_True; }
63 else if (anArgument == "shapeview") { thePluginName = "TKShapeView"; return Standard_True; }
64 else if (anArgument == "vinspector") { thePluginName = "TKVInspector"; return Standard_True; }
66 return Standard_False;
69 // =======================================================================
70 // function : getArgumentPlugins
71 // purpose : fills container of plugin names by the next following plugin names
72 // =======================================================================
73 void getArgumentPlugins (Standard_Integer theArgsNb, const char** theArgs, Standard_Integer& theIt,
74 NCollection_List<TCollection_AsciiString>& thePlugins)
76 while (theIt != theArgsNb)
78 TCollection_AsciiString aPluginName;
79 if (convertToPluginName (theArgs[theIt], aPluginName))
81 if (!thePlugins.Contains (aPluginName))
82 thePlugins.Append (aPluginName);
90 theIt--; // the last not processed parameter is the next argument
93 // =======================================================================
94 // function : tinspector
96 // =======================================================================
97 static int tinspector (Draw_Interpretor& di, Standard_Integer theArgsNb, const char** theArgs)
101 std::cout << "Error: wrong number of arguments.\n";
105 // parse command arguments
106 NCollection_List<TCollection_AsciiString> aPlugins;
107 NCollection_DataMap<TCollection_AsciiString, NCollection_List<Handle(Standard_Transient)> > aParameters;
108 NCollection_DataMap<TCollection_AsciiString, TCollection_AsciiString > anOpenFileParameters;
109 TCollection_AsciiString aPluginNameToActivate;
110 Standard_Boolean aNeedToUpdateContent = Standard_False,
111 aNeedToHideInspector = Standard_False,
112 aNeedToShowInspector = Standard_False,
113 aNeedToPrintState = Standard_False,
114 aNeedDirectory = Standard_False;
115 TCollection_AsciiString aTemporaryDirectory;
117 NCollection_List<Handle(Standard_Transient)> aDefaultParameters;
118 TCollection_AsciiString aDefaultOpenFileParameter;
120 NCollection_List<Handle(Standard_Transient)> anObjectsToSelect;
121 NCollection_List<TCollection_AsciiString> anItemNamesToSelect;
123 for (Standard_Integer anIt = 1; anIt < theArgsNb; ++anIt)
125 TCollection_AsciiString aParam (theArgs[anIt]);
128 if (aParam.IsEqual ("-plugins")) // [-plugins {name1 [name2] ... [name3] | all}]
131 getArgumentPlugins (theArgsNb, theArgs, anIt, aPlugins);
133 else if (aParam.IsEqual ("-activate")) // [-activate name]
136 if (anIt == theArgsNb)
138 std::cout << "Empty argument of '" << aParam << "'.\n";
141 TCollection_AsciiString aPluginName;
142 if (convertToPluginName (theArgs[anIt], aPluginName))
143 aPluginNameToActivate = aPluginName;
145 else if (aParam.IsEqual ("-shape")) // [-shape object [name1] ... [nameN]]
148 if (anIt == theArgsNb)
150 std::cout << "Empty argument of '" << aParam << "'.\n";
153 TopoDS_Shape aShape = DBRep::Get (theArgs[anIt]);
157 std::cout << "Wrong shape name: " << aParam << ".\n";
160 NCollection_List<TCollection_AsciiString> anArgPlugins;
161 getArgumentPlugins (theArgsNb, theArgs, anIt, anArgPlugins);
163 if (anArgPlugins.IsEmpty())
165 aDefaultParameters.Append (aShape.TShape());
166 anItemNamesToSelect.Append (TInspectorAPI_PluginParameters::ParametersToString (aShape));
170 for (NCollection_List<TCollection_AsciiString>::Iterator anArgIt (anArgPlugins);
171 anArgIt.More(); anArgIt.Next())
173 NCollection_List<Handle(Standard_Transient)> aPluginParameters;
174 aParameters.Find (anArgIt.Value(), aPluginParameters);
175 aPluginParameters.Append (aShape.TShape());
176 anItemNamesToSelect.Append (TInspectorAPI_PluginParameters::ParametersToString (aShape));
177 aParameters.Bind (anArgIt.Value(), aPluginParameters);
181 else if (aParam.IsEqual ("-open")) // [-open file_name [name1] ... [nameN]]
184 if (anIt == theArgsNb)
186 std::cout << "Empty argument of '" << aParam << "'.\n";
189 TCollection_AsciiString aFileName (theArgs[anIt]);
192 NCollection_List<TCollection_AsciiString> anArgPlugins;
193 getArgumentPlugins(theArgsNb, theArgs, anIt, anArgPlugins);
194 if (anArgPlugins.IsEmpty())
195 aDefaultOpenFileParameter = aFileName;
198 for (NCollection_List<TCollection_AsciiString>::Iterator anArgIt (anArgPlugins);
199 anArgIt.More(); anArgIt.Next())
201 NCollection_List<Handle(Standard_Transient)> aPluginParameters;
202 aParameters.Find (anArgIt.Value(), aPluginParameters);
203 anOpenFileParameters.Bind (anArgIt.Value(), aFileName);
207 else if (aParam.IsEqual ("-directory")) // [-directory path]"
210 if (anIt == theArgsNb)
212 std::cout << "Empty argument of '" << aParam << "'.\n";
215 aNeedDirectory = true;
216 aParam = theArgs[anIt];
217 aTemporaryDirectory = aParam.IsEqual ("default") ? "" : aParam;
219 else if (aParam.IsEqual ("-state")) // [-state]
221 aNeedToPrintState = Standard_True;
223 else if (aParam.IsEqual ("-update")) // [-update]
225 aNeedToUpdateContent = Standard_True;
227 else if (aParam.IsEqual ("-select")) // [-select {name|object}]
230 if (anIt == theArgsNb)
232 std::cout << "Empty argument of '" << aParam << "'.\n";
235 // search shape with given name
236 TopoDS_Shape aShape = DBRep::Get (theArgs[anIt]);
237 if (!aShape.IsNull())
239 anObjectsToSelect.Append (aShape.TShape());
240 anItemNamesToSelect.Append (TInspectorAPI_PluginParameters::ParametersToString (aShape));
242 // search prsentations with given name
243 Handle(AIS_InteractiveObject) anIO;
244 GetMapOfAIS().Find2 (theArgs[anIt], anIO);
247 anObjectsToSelect.Append (anIO);
249 // give parameters as a container of names
250 aParam = TCollection_AsciiString (theArgs[anIt]);
251 while (!aParam.StartsWith ("-"))
253 anItemNamesToSelect.Append (aParam);
255 if (anIt >= theArgsNb)
257 aParam = theArgs[anIt];
261 else if (aParam.IsEqual ("-show")) // [-show {0|1} = 1]
264 if (anIt == theArgsNb)
266 std::cout << "Empty argument of '" << aParam << "'.\n";
269 aNeedToHideInspector = Draw::Atoi (theArgs[anIt]) == 0;
270 aNeedToShowInspector = Draw::Atoi (theArgs[anIt]) > 0;
274 std::cout << "Wrong argument of command: " << aParam.ToCString() << "\n";
280 Standard_Boolean isTInspectorCreation = !MyCommunicator;
282 MyCommunicator = new TInspector_Communicator();
284 Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
285 if (!aContext.IsNull())
286 aDefaultParameters.Append (aContext);
288 // Sets OCAF application into DFBrowser
289 const Handle(TDocStd_Application)& anApplication = DDocStd::GetApplication();
290 // Initialize standard document formats at creation - they should
291 // be available even if this DRAW plugin is not loaded by pload command
292 if (!anApplication.IsNull())
294 NCollection_List<Handle(Standard_Transient)> aDFBrowserParameters;
295 aParameters.Find ("TKDFBrowser", aDFBrowserParameters);
296 aDFBrowserParameters.Append (anApplication);
297 aParameters.Bind ("TKDFBrowser", aDFBrowserParameters);
300 // by starting, if the plugns were not defined, register all
301 if (isTInspectorCreation)
303 if (aPlugins.IsEmpty())
305 aPlugins.Append ("TKDFBrowser");
306 aPlugins.Append ("TKShapeView");
307 aPlugins.Append ("TKVInspector");
309 aPluginNameToActivate = !aPluginNameToActivate.IsEmpty() ? aPluginNameToActivate : aPlugins.First();
312 // register plugin from parameters
313 for (NCollection_List<TCollection_AsciiString>::Iterator aPluginNameIt (aPlugins);
314 aPluginNameIt.More(); aPluginNameIt.Next())
315 MyCommunicator->RegisterPlugin (aPluginNameIt.Value());
317 // init all registered plugins with the default and parameters values
318 NCollection_List<TCollection_AsciiString> aRegisteredPlugins = MyCommunicator->RegisteredPlugins();
319 for (NCollection_List<TCollection_AsciiString>::Iterator anIterator (aRegisteredPlugins);
320 anIterator.More(); anIterator.Next())
322 TCollection_AsciiString aPluginName = anIterator.Value();
323 NCollection_List<Handle(Standard_Transient)> aParameterValues;
324 aParameters.Find (aPluginName, aParameterValues);
326 for (NCollection_List<Handle(Standard_Transient)>::Iterator aDefIt (aDefaultParameters);
327 aDefIt.More(); aDefIt.Next())
328 aParameterValues.Append (aDefIt.Value());
329 MyCommunicator->Init (aPluginName, aParameterValues, Standard_True);
332 if (!aPluginNameToActivate.IsEmpty())
333 MyCommunicator->Activate (!aPluginNameToActivate.IsEmpty() ? aPluginNameToActivate : aPlugins.First());
335 if (!anOpenFileParameters.IsEmpty())
337 for (NCollection_DataMap<TCollection_AsciiString, TCollection_AsciiString >::Iterator anOpenIt
338 (anOpenFileParameters); anOpenIt.More(); anOpenIt.Next())
339 MyCommunicator->OpenFile (anOpenIt.Key(), anOpenIt.Value());
341 else if (!aDefaultOpenFileParameter.IsEmpty()) // open file in active plugin
342 MyCommunicator->OpenFile ("", aDefaultOpenFileParameter);
344 if (!anObjectsToSelect.IsEmpty())
345 MyCommunicator->SetSelected (anObjectsToSelect);
347 if (!anItemNamesToSelect.IsEmpty())
348 MyCommunicator->SetSelected (anItemNamesToSelect);
351 MyCommunicator->SetTemporaryDirectory (aTemporaryDirectory);
353 if (aNeedToUpdateContent)
354 MyCommunicator->UpdateContent();
356 if (isTInspectorCreation || aNeedToShowInspector)
357 MyCommunicator->SetVisible (true);
359 if (aNeedToHideInspector)
360 MyCommunicator->SetVisible (false);
362 if (aNeedToPrintState)
364 Standard_SStream aSStream;
365 MyCommunicator->Dump (aSStream);
366 di << aSStream << "\n";
372 // =======================================================================
373 // function : Commands
375 // =======================================================================
376 void ToolsDraw::Commands(Draw_Interpretor& theCommands)
378 const char *group = "Tools";
381 theCommands.Add ("tinspector",
382 "tinspector [-plugins {name1 ... [nameN] | all}]"
383 "\n\t\t: [-activate name]"
384 "\n\t\t: [-shape object [name1] ... [nameN]]"
385 "\n\t\t: [-open file_name [name1] ... [nameN]]"
387 "\n\t\t: [-select {object | name1 ... [nameN]}]"
388 "\n\t\t: [-show {0|1} = 1]"
389 "\n\t\t: [-directory path|<default>]"
391 "\n\t\t: Starts tool of inspection."
393 "\n\t\t: -plugins enters plugins that should be added in the inspector."
394 "\n\t\t: Available names are: dfbrowser, vinspector and shapeview."
395 "\n\t\t: Plugins order will be the same as defined in arguments."
396 "\n\t\t: 'all' adds all available plugins in the order:"
397 "\n\t\t: DFBrowser, VInspector and ShapeView."
398 "\n\t\t: If at the first call this option is not used, 'all' option is applied;"
399 "\n\t\t: -activate activates the plugin in the tool view."
400 "\n\t\t: If at the first call this option is not used, the first plugin is activated;"
401 "\n\t\t: -shape initializes plugin/s by the shape object. If 'name' is empty, initializes all plugins;"
402 "\n\t\t: -open gives the file to the plugin/s. If the plugin is active, after open, update content will be done;"
403 "\n\t\t: -update updates content of the active plugin;"
404 "\n\t\t: -select sets the parameter that should be selected in an active tool view."
405 "\n\t\t: Depending on active tool the parameter is:"
406 "\n\t\t: ShapeView: 'object' is an instance of TopoDS_Shape TShape,"
407 "\n\t\t: DFBrowser: 'name' is an entry of TDF_Label and name2(optionaly) for TDF_Attribute type name,"
408 "\n\t\t: VInspector: 'object' is an instance of AIS_InteractiveObject;"
409 "\n\t\t: -show sets Inspector view visible or hidden. The first call of this command will show it."
410 "\n\t\t: -directory sets Inspector temporary directory. Preferences file is stored there."
411 "\n\t\t: -state print some current information about inspector, like name of active plugin, temporary director.",
412 __FILE__, tinspector, group);
416 // =======================================================================
417 // function : Factory
419 // =======================================================================
420 void ToolsDraw::Factory (Draw_Interpretor& theDI)
422 // definition of Tools Command
423 ToolsDraw::Commands (theDI);
426 theDI << "Draw Plugin : OCC Tools commands are loaded\n";
430 // Declare entry point PLUGINFACTORY