1
0
mirror of https://git.dev.opencascade.org/repos/occt.git synced 2025-08-19 13:40:49 +03:00

0031709: Draw Harness - move methods ViewerTest::ParseOnOff()/ParseColor() to package Draw

Methods ParseOnOff()/ParseColor() have been moved from package ViewerTest to Draw.
Command "vlight -color" now accepts RGB values, not only name.
Implementation of pload command has been cleaned up.
This commit is contained in:
kgv
2020-08-12 15:58:22 +03:00
committed by bugmaster
parent 76fada6839
commit dae2a92241
14 changed files with 851 additions and 774 deletions

View File

@@ -675,31 +675,29 @@ Standard_Integer Draw_Call (char *c)
//=================================================================================
//
//=================================================================================
void Draw::Load(Draw_Interpretor& theDI, const TCollection_AsciiString& theKey,
const TCollection_AsciiString& theResourceFileName,
TCollection_AsciiString& theDefaultsDirectory,
TCollection_AsciiString& theUserDefaultsDirectory,
const Standard_Boolean Verbose ) {
void Draw::Load (Draw_Interpretor& theDI,
const TCollection_AsciiString& theKey,
const TCollection_AsciiString& theResourceFileName,
const TCollection_AsciiString& theDefaultsDirectory,
const TCollection_AsciiString& theUserDefaultsDirectory,
const Standard_Boolean theIsVerbose)
{
static Plugin_MapOfFunctions theMapOfFunctions;
OSD_Function f;
if(!theMapOfFunctions.IsBound(theKey)) {
Handle(Resource_Manager) aPluginResource = new Resource_Manager(theResourceFileName.ToCString(), theDefaultsDirectory, theUserDefaultsDirectory, Verbose);
if(!aPluginResource->Find(theKey.ToCString())) {
Standard_SStream aMsg; aMsg << "Could not find the resource:";
aMsg << theKey.ToCString()<< std::endl;
std::cout << "could not find the resource:"<<theKey.ToCString()<< std::endl;
throw Draw_Failure(aMsg.str().c_str());
OSD_Function aFunc = NULL;
if (!theMapOfFunctions.Find (theKey, aFunc))
{
TCollection_AsciiString aPluginLibrary;
Handle(Resource_Manager) aPluginResource = new Resource_Manager (theResourceFileName, theDefaultsDirectory, theUserDefaultsDirectory, theIsVerbose);
if (!aPluginResource->Find (theKey, aPluginLibrary))
{
Message::SendFail() << "could not find the resource:" << theKey;
Standard_SStream aMsg; aMsg << "Could not find the resource:" << theKey << std::endl;
throw Draw_Failure (aMsg.str().c_str());
}
TCollection_AsciiString aPluginLibrary("");
#if !defined(_WIN32) || defined(__MINGW32__)
aPluginLibrary += "lib";
aPluginLibrary = TCollection_AsciiString ("lib") + aPluginLibrary;
#endif
aPluginLibrary += aPluginResource->Value(theKey.ToCString());
#ifdef _WIN32
aPluginLibrary += ".dll";
#elif __APPLE__
@@ -709,37 +707,251 @@ void Draw::Load(Draw_Interpretor& theDI, const TCollection_AsciiString& theKey,
#else
aPluginLibrary += ".so";
#endif
OSD_SharedLibrary aSharedLibrary(aPluginLibrary.ToCString());
if(!aSharedLibrary.DlOpen(OSD_RTLD_LAZY)) {
TCollection_AsciiString error(aSharedLibrary.DlError());
Standard_SStream aMsg; aMsg << "Could not open: ";
aMsg << aPluginResource->Value(theKey.ToCString());
aMsg << "; reason: ";
aMsg << error.ToCString();
OSD_SharedLibrary aSharedLibrary (aPluginLibrary.ToCString());
if (!aSharedLibrary.DlOpen (OSD_RTLD_LAZY))
{
const TCollection_AsciiString anError (aSharedLibrary.DlError());
Standard_SStream aMsg;
aMsg << "Could not open: " << aPluginLibrary << "; reason: " << anError;
#ifdef OCCT_DEBUG
std::cout << "could not open: " << aPluginResource->Value(theKey.ToCString())<< " ; reason: "<< error.ToCString() << std::endl;
std::cout << "could not open: " << aPluginLibrary << " ; reason: "<< anError << std::endl;
#endif
throw Draw_Failure(aMsg.str().c_str());
}
f = aSharedLibrary.DlSymb("PLUGINFACTORY");
if( f == NULL ) {
TCollection_AsciiString error(aSharedLibrary.DlError());
Standard_SStream aMsg; aMsg << "Could not find the factory in: ";
aMsg << aPluginResource->Value(theKey.ToCString());
aMsg << error.ToCString();
aFunc = aSharedLibrary.DlSymb ("PLUGINFACTORY");
if (aFunc == NULL)
{
const TCollection_AsciiString anError (aSharedLibrary.DlError());
Standard_SStream aMsg;
aMsg << "Could not find the factory in: " << aPluginLibrary << anError;
throw Draw_Failure(aMsg.str().c_str());
}
theMapOfFunctions.Bind(theKey, f);
theMapOfFunctions.Bind (theKey, aFunc);
}
else
f = theMapOfFunctions(theKey);
// void (*fp) (Draw_Interpretor&, const TCollection_AsciiString&) = NULL;
// fp = (void (*)(Draw_Interpretor&, const TCollection_AsciiString&)) f;
// (*fp) (theDI, theKey);
void (*fp) (Draw_Interpretor&) = NULL;
fp = (void (*)(Draw_Interpretor&)) f;
fp = (void (*)(Draw_Interpretor&) )aFunc;
(*fp) (theDI);
}
namespace
{
const Standard_Integer THE_MAX_INTEGER_COLOR_COMPONENT = 255;
const Standard_ShortReal THE_MAX_REAL_COLOR_COMPONENT = 1.0f;
//! Parses string and get an integer color component (only values within range 0 .. 255 are allowed)
//! @param theColorComponentString the string representing the color component
//! @param theIntegerColorComponent an integer color component that is a result of parsing
//! @return true if parsing was successful, or false otherwise
static bool parseNumericalColorComponent (const Standard_CString theColorComponentString,
Standard_Integer& theIntegerColorComponent)
{
Standard_Integer anIntegerColorComponent;
if (!Draw::ParseInteger (theColorComponentString, anIntegerColorComponent))
{
return false;
}
if ((anIntegerColorComponent < 0) || (anIntegerColorComponent > THE_MAX_INTEGER_COLOR_COMPONENT))
{
return false;
}
theIntegerColorComponent = anIntegerColorComponent;
return true;
}
//! Parses the string and gets a real color component from it (only values within range 0.0 .. 1.0 are allowed)
//! @param theColorComponentString the string representing the color component
//! @param theRealColorComponent a real color component that is a result of parsing
//! @return true if parsing was successful, or false otherwise
static bool parseNumericalColorComponent (const Standard_CString theColorComponentString,
Standard_ShortReal& theRealColorComponent)
{
Standard_Real aRealColorComponent;
if (!Draw::ParseReal (theColorComponentString, aRealColorComponent))
{
return false;
}
const Standard_ShortReal aShortRealColorComponent = static_cast<Standard_ShortReal> (aRealColorComponent);
if ((aShortRealColorComponent < 0.0f) || (aShortRealColorComponent > THE_MAX_REAL_COLOR_COMPONENT))
{
return false;
}
theRealColorComponent = aShortRealColorComponent;
return true;
}
//! Parses the string and gets a real color component from it (integer values 2 .. 255 are scaled to the 0.0 .. 1.0
//! range, values 0 and 1 are leaved as they are)
//! @param theColorComponentString the string representing the color component
//! @param theColorComponent a color component that is a result of parsing
//! @return true if parsing was successful, or false otherwise
static bool parseColorComponent (const Standard_CString theColorComponentString,
Standard_ShortReal& theColorComponent)
{
Standard_Integer anIntegerColorComponent;
if (parseNumericalColorComponent (theColorComponentString, anIntegerColorComponent))
{
if (anIntegerColorComponent == 1)
{
theColorComponent = THE_MAX_REAL_COLOR_COMPONENT;
}
else
{
theColorComponent = anIntegerColorComponent * 1.0f / THE_MAX_INTEGER_COLOR_COMPONENT;
}
return true;
}
return parseNumericalColorComponent (theColorComponentString, theColorComponent);
}
//! Parses the array of strings and gets an integer color (only values within range 0 .. 255 are allowed and at least
//! one of components must be greater than 1)
//! @tparam TheNumber the type of resulting color vector elements
//! @param theNumberOfColorComponents the number of color components
//! @param theColorComponentStrings the array of strings representing color components
//! @param theNumericalColor a 4-component vector that is a result of parsing
//! @return true if parsing was successful, or false otherwise
template <typename TheNumber>
static bool parseNumericalColor (Standard_Integer& theNumberOfColorComponents,
const char* const* const theColorComponentStrings,
NCollection_Vec4<TheNumber>& theNumericalColor)
{
for (Standard_Integer aColorComponentIndex = 0; aColorComponentIndex < theNumberOfColorComponents;
++aColorComponentIndex)
{
const char* const aColorComponentString = theColorComponentStrings[aColorComponentIndex];
TheNumber aNumericalColorComponent;
if (parseNumericalColorComponent (aColorComponentString, aNumericalColorComponent))
{
theNumericalColor[aColorComponentIndex] = aNumericalColorComponent;
}
else
{
if (aColorComponentIndex == 3)
{
theNumberOfColorComponents = 3;
}
else
{
return false;
}
}
}
return true;
}
//! Parses an array of strings and get an integer color (only values within range 0 .. 255 are allowed and at least
//! one of components must be greater than 1)
//! @param theNumberOfColorComponents the number of color components
//! @param theColorComponentStrings the array of strings representing color components
//! @param theColor a color that is a result of parsing
//! @return true if parsing was successful, or false otherwise
static bool parseIntegerColor (Standard_Integer& theNumberOfColorComponents,
const char* const* const theColorComponentStrings,
Quantity_ColorRGBA& theColor)
{
const Standard_Integer THE_COLOR_COMPONENT_NOT_PARSED = -1;
NCollection_Vec4<int> anIntegerColor (THE_COLOR_COMPONENT_NOT_PARSED);
if (!parseNumericalColor (theNumberOfColorComponents, theColorComponentStrings, anIntegerColor)
|| anIntegerColor.maxComp() <= 1)
{
return false;
}
if (anIntegerColor.a() == THE_COLOR_COMPONENT_NOT_PARSED)
{
anIntegerColor.a() = THE_MAX_INTEGER_COLOR_COMPONENT;
}
const NCollection_Vec4<float> aRealColor = NCollection_Vec4<float> (anIntegerColor) / static_cast<float> (THE_MAX_INTEGER_COLOR_COMPONENT);
theColor = Quantity_ColorRGBA (Quantity_ColorRGBA::Convert_sRGB_To_LinearRGB (aRealColor));
return true;
}
//! Parses an array of strings and get a real color (only values within range 0.0 .. 1.0 are allowed)
//! @param theNumberOfColorComponents the number of color components
//! @param theColorComponentStrings the array of strings representing color components
//! @param theColor a color that is a result of parsing
//! @return true if parsing was successful, or false otherwise
static bool parseRealColor (Standard_Integer& theNumberOfColorComponents,
const char* const* const theColorComponentStrings,
Quantity_ColorRGBA& theColor)
{
NCollection_Vec4<float> aRealColor (THE_MAX_REAL_COLOR_COMPONENT);
if (!parseNumericalColor (theNumberOfColorComponents, theColorComponentStrings, aRealColor))
{
return false;
}
theColor = Quantity_ColorRGBA (aRealColor);
return true;
}
}
//=======================================================================
// function : parseColor
// purpose :
//=======================================================================
Standard_Integer Draw::parseColor (const Standard_Integer theArgNb,
const char* const* const theArgVec,
Quantity_ColorRGBA& theColor,
const bool theToParseAlpha)
{
if ((theArgNb >= 1) && Quantity_ColorRGBA::ColorFromHex (theArgVec[0], theColor, !theToParseAlpha))
{
return 1;
}
if (theArgNb >= 1 && Quantity_ColorRGBA::ColorFromName (theArgVec[0], theColor))
{
if (theArgNb >= 2 && theToParseAlpha)
{
const Standard_CString anAlphaStr = theArgVec[1];
Standard_ShortReal anAlphaComponent;
if (parseColorComponent (anAlphaStr, anAlphaComponent))
{
theColor.SetAlpha (anAlphaComponent);
return 2;
}
}
return 1;
}
if (theArgNb >= 3)
{
const Standard_Integer aNumberOfColorComponentsToParse = Min (theArgNb, theToParseAlpha ? 4 : 3);
Standard_Integer aNumberOfColorComponentsParsed = aNumberOfColorComponentsToParse;
if (parseIntegerColor (aNumberOfColorComponentsParsed, theArgVec, theColor))
{
return aNumberOfColorComponentsParsed;
}
aNumberOfColorComponentsParsed = aNumberOfColorComponentsToParse;
if (parseRealColor (aNumberOfColorComponentsParsed, theArgVec, theColor))
{
return aNumberOfColorComponentsParsed;
}
return 0;
}
return 0;
}
//=======================================================================
//function : ParseOnOff
//purpose :
//=======================================================================
Standard_Boolean Draw::ParseOnOff (Standard_CString theArg,
Standard_Boolean& theIsOn)
{
TCollection_AsciiString aFlag(theArg);
aFlag.LowerCase();
if (aFlag == "on"
|| aFlag == "1")
{
theIsOn = Standard_True;
return Standard_True;
}
else if (aFlag == "off"
|| aFlag == "0")
{
theIsOn = Standard_False;
return Standard_True;
}
return Standard_False;
}

View File

@@ -17,39 +17,12 @@
#ifndef _Draw_HeaderFile
#define _Draw_HeaderFile
#include <Standard.hxx>
#include <Standard_DefineAlloc.hxx>
#include <Draw_Interpretor.hxx>
#include <Quantity_ColorRGBA.hxx>
#include <Standard_Handle.hxx>
#include <Draw_Interpretor.hxx>
#include <Standard_Boolean.hxx>
#include <Standard_CString.hxx>
#include <Standard_Real.hxx>
#include <Standard_Integer.hxx>
class TCollection_AsciiString;
class Draw_Drawable3D;
class Draw_ProgressIndicator;
class Draw_Drawable3D;
class Draw_Drawable2D;
class Draw_Color;
class Draw_Display;
class Draw_Segment3D;
class Draw_Segment2D;
class Draw_Marker3D;
class Draw_Marker2D;
class Draw_Axis3D;
class Draw_Axis2D;
class Draw_Text3D;
class Draw_Text2D;
class Draw_Circle3D;
class Draw_Circle2D;
class Draw_Number;
class Draw_Chronometer;
class Draw_Grid;
class Draw_Box;
class Draw_ProgressIndicator;
class Draw_Printer;
//! MAQUETTE DESSIN MODELISATION
class Draw
@@ -58,9 +31,22 @@ public:
DEFINE_STANDARD_ALLOC
Standard_EXPORT static void Load (Draw_Interpretor& theDI, const TCollection_AsciiString& theKey, const TCollection_AsciiString& theResourceFileName, TCollection_AsciiString& theDefaultsDirectory, TCollection_AsciiString& theUserDefaultsDirectory, const Standard_Boolean Verbose = Standard_False);
//! (Re)Load a Draw Harness plugin.
//! @param theDI [in] [out] Tcl interpretor to append loaded commands
//! @param theKey [in] plugin code name to be resolved in resource file
//! @param theResourceFileName [in] description file name
//! @param theDefaultsDirectory [in] default folder for looking description file
//! @param theUserDefaultsDirectory [in] user folder for looking description file
//! @param theIsVerbose [in] print verbose messages
Standard_EXPORT static void Load (Draw_Interpretor& theDI,
const TCollection_AsciiString& theKey,
const TCollection_AsciiString& theResourceFileName,
const TCollection_AsciiString& theDefaultsDirectory,
const TCollection_AsciiString& theUserDefaultsDirectory,
const Standard_Boolean theIsVerbose = Standard_False);
public: //! @name Tcl variables management tools
//! Sets a variable. Display it if <Disp> is true.
Standard_EXPORT static void Set (const Standard_CString Name, const Handle(Draw_Drawable3D)& D, const Standard_Boolean Disp);
@@ -91,15 +77,17 @@ public:
//! Sets a TCL sting variable
Standard_EXPORT static void Set (const Standard_CString Name, const Standard_CString val);
public: //! @name argument parsing tools
//! Converts numeric expression, that can involve DRAW
//! variables, to real value.
Standard_EXPORT static Standard_Real Atof (const Standard_CString Name);
//! Converts the numeric expression, that can involve DRAW variables, to a real value
//! @param theExpressionString the strings that containes the expression involving DRAW variables to be parsed
//! @param theExpressionString the strings that contains the expression involving DRAW variables to be parsed
//! @param theParsedRealValue a real value that is a result of parsing
//! @return true if parsing was successfull, or false otherwise
//! @return true if parsing was successful, or false otherwise
Standard_EXPORT static bool ParseReal (const Standard_CString theExpressionString, Standard_Real& theParsedRealValue);
//! Converts numeric expression, that can involve DRAW
@@ -108,12 +96,91 @@ public:
Standard_EXPORT static Standard_Integer Atoi (const Standard_CString Name);
//! Converts the numeric expression, that can involve DRAW variables, to an integer value
//! @param theExpressionString the strings that containes the expression involving DRAW variables to be parsed
//! @param theExpressionString the strings that contains the expression involving DRAW variables to be parsed
//! @param theParsedIntegerValue an integer value that is a result of parsing
//! @return true if parsing was successfull, or false otherwise
//! @return true if parsing was successful, or false otherwise
Standard_EXPORT static bool ParseInteger (const Standard_CString theExpressionString,
Standard_Integer& theParsedIntegerValue);
//! Parses RGB(A) color argument(s) specified within theArgVec[0], theArgVec[1], theArgVec[2] and theArgVec[3].
//! Handles either color specified by name (single argument) or by RGB(A) components (3-4 arguments) in range 0..1.
//! The result is stored in theColor on success.
//!
//! Usage code sample for command argument in form "cmd -color {ColorName|R G B [A]|ColorHex}":
//! @code
//! for (int anArgIter = 1; anArgIter < theNbArgs; ++anArgIter)
//! {
//! TCollection_AsciiString aParam (theArgVec[anArgIter]);
//! aParam.LowerCase();
//! if (aParam == "-color")
//! {
//! Quantity_ColorRGBA aColor;
//! Standard_Integer aNbParsed = Draw::ParseColor (theArgNb - anArgIter - 1,
//! theArgVec + anArgIter + 1, aColor);
//! anArgIter += aNbParsed;
//! if (aNbParsed == 0) { std::cerr << "Syntax error at '" << aParam << "'"; return 1; }
//! // process color
//! }
//! }
//! @endcode
//!
//! @param theArgNb [in] number of available arguments in theArgVec (array limits)
//! @param theArgVec [in] argument list
//! @param theColor [out] retrieved color
//! @return number of handled arguments (1, 2, 3 or 4) or 0 on syntax error
static Standard_Integer ParseColor (const Standard_Integer theArgNb,
const char* const* const theArgVec,
Quantity_ColorRGBA& theColor)
{
return parseColor (theArgNb, theArgVec, theColor, true);
}
//! Parses RGB color argument(s).
//! @param theArgNb [in] number of available arguments in theArgVec (array limits)
//! @param theArgVec [in] argument list
//! @param theColor [out] retrieved color
//! @return number of handled arguments (1 or 3) or 0 on syntax error.
static Standard_Integer ParseColor (const Standard_Integer theArgNb,
const char* const* const theArgVec,
Quantity_Color& theColor)
{
Quantity_ColorRGBA anRgba;
const Standard_Integer aNbParsed = parseColor (theArgNb, theArgVec, anRgba, false);
if (aNbParsed != 0)
{
theColor = anRgba.GetRGB();
}
return aNbParsed;
}
//! Parses boolean argument. Handles either flag specified by 0|1 or on|off.
//!
//! Usage code sample for command argument in form "cmd -usefeature [on|off|1|0]=on":
//! @code
//! for (int anArgIter = 1; anArgIter < theNbArgs; ++anArgIter)
//! {
//! TCollection_AsciiString aParam (theArgVec[anArgIter]);
//! aParam.LowerCase();
//! if (aParam == "-usefeature")
//! {
//! bool toUseFeature = true;
//! if (anArgIter + 1 < theNbArgs && Draw::ParseOnOff (theArgVec[anArgIter + 1]))
//! {
//! ++anArgIter;
//! }
//! // process feature
//! }
//! }
//! @endcode
//!
//! @param theArg [in] argument value
//! @param theIsOn [out] decoded Boolean flag
//! @return FALSE on syntax error
Standard_EXPORT static Standard_Boolean ParseOnOff (Standard_CString theArg,
Standard_Boolean& theIsOn);
public:
//! Returns last graphic selection description.
Standard_EXPORT static void LastPick (Standard_Integer& view, Standard_Integer& X, Standard_Integer& Y, Standard_Integer& button);
@@ -125,7 +192,9 @@ public:
//! gets progress indicator
Standard_EXPORT static Handle(Draw_ProgressIndicator) GetProgressBar();
public: //! @name methods loading standard command sets
//! Defines all Draw commands
Standard_EXPORT static void Commands (Draw_Interpretor& I);
@@ -152,35 +221,16 @@ protected:
Standard_EXPORT static Handle(Draw_Drawable3D) getDrawable (Standard_CString& theName,
Standard_Boolean theToAllowPick);
private:
friend class Draw_Drawable3D;
friend class Draw_Drawable2D;
friend class Draw_Color;
friend class Draw_Display;
friend class Draw_Segment3D;
friend class Draw_Segment2D;
friend class Draw_Marker3D;
friend class Draw_Marker2D;
friend class Draw_Axis3D;
friend class Draw_Axis2D;
friend class Draw_Text3D;
friend class Draw_Text2D;
friend class Draw_Circle3D;
friend class Draw_Circle2D;
friend class Draw_Number;
friend class Draw_Chronometer;
friend class Draw_Grid;
friend class Draw_Box;
friend class Draw_ProgressIndicator;
friend class Draw_Printer;
//! Parses RGB(A) color argument(s) specified within theArgVec[0], theArgVec[1], theArgVec[2] and theArgVec[3].
//! Handles either color specified by name (single argument)
//! or by RGB(A) components (3-4 arguments) in range 0..1.
//! The result is stored in theColor on success.
//! Returns number of handled arguments (1, 2, 3 or 4) or 0 on syntax error.
Standard_EXPORT static Standard_Integer parseColor (Standard_Integer theArgNb,
const char* const* theArgVec,
Quantity_ColorRGBA& theColor,
bool theToParseAlpha);
};
#endif // _Draw_HeaderFile

View File

@@ -13,152 +13,156 @@
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
#include <TCollection_AsciiString.hxx>
#include <Draw_Interpretor.hxx>
#include <Draw_MapOfAsciiString.hxx>
#include <Draw.hxx>
#include <Message.hxx>
#include <OSD_Path.hxx>
#include <OSD_Directory.hxx>
#include <OSD_File.hxx>
#include <OSD_Environment.hxx>
#include <OSD_SharedLibrary.hxx>
#include <Resource_Manager.hxx>
#include <Draw_Interpretor.hxx>
#include <Draw_MapOfAsciiString.hxx>
#include <Draw.hxx>
#include <TCollection_AsciiString.hxx>
static Handle(Resource_Manager) myResources;
//=======================================================================
//function : FindPluginFile
//purpose : Searches for the existence of the plugin file according to its name thePluginName:
// - if thePluginName is empty then it defaults to DrawPlugin
// - the search directory is defined according to the variable
// CSF_<filename>Defaults (if it is omitted then it defaults to
// $CASROOT/src/DrawResources)
// - finally existence of the file is verified in the search directory
// - if the file exists but corresponding variable (CSF_...) has not been
// explicitly set, it is forced to (for further reuse by Resource_Manager)
// Returns True if the file exists, otherwise - False.
//=======================================================================
#define FAILSTR "Failed to load plugin: "
//static Standard_Boolean FindPluginFile (TCollection_AsciiString& thePluginName)
static Standard_Boolean FindPluginFile (TCollection_AsciiString& thePluginName, TCollection_AsciiString& aPluginDir)
//! Searches for the existence of the plugin file according to its name thePluginName:
//! - if thePluginName is empty then it defaults to DrawPlugin
//! - the search directory is defined according to the variable
//! CSF_<filename>Defaults (if it is omitted then it defaults to
//! $CASROOT/src/DrawResources)
//! - finally existence of the file is verified in the search directory
//! - if the file exists but corresponding variable (CSF_...) has not been
//! explicitly set, it is forced to (for further reuse by Resource_Manager)
//! @return TRUE if the file exists, otherwise - False
static Standard_Boolean findPluginFile (TCollection_AsciiString& thePluginName,
TCollection_AsciiString& thePluginDir)
{
Standard_Boolean aResult = Standard_True;
// check if the file name has been specified and use default value if not
if (thePluginName.IsEmpty()) {
if (thePluginName.IsEmpty())
{
thePluginName += "DrawPlugin";
#ifdef OCCT_DEBUG
std::cout << "Plugin file name has not been specified. Defaults to " << thePluginName.ToCString() << std::endl;
#endif
}
//TCollection_AsciiString aPluginDir; // the search directory
Standard_Boolean aDirFound = Standard_True, aToSetCSFVariable = Standard_False;
Standard_Boolean aToSetCSFVariable = Standard_False;
// the order of search : by CSF_<PluginFileName>Defaults and then by CASROOT
TCollection_AsciiString aCSFVariable = TCollection_AsciiString ("CSF_") + thePluginName + "Defaults";
aPluginDir = OSD_Environment (aCSFVariable).Value();
if (aPluginDir.IsEmpty())
const TCollection_AsciiString aCSFVariable = TCollection_AsciiString ("CSF_") + thePluginName + "Defaults";
thePluginDir = OSD_Environment (aCSFVariable).Value();
if (thePluginDir.IsEmpty())
{
aPluginDir = OSD_Environment ("DRAWHOME").Value();
if (!aPluginDir.IsEmpty())
thePluginDir = OSD_Environment ("DRAWHOME").Value();
if (!thePluginDir.IsEmpty())
{
aToSetCSFVariable = Standard_True; //CSF variable to be set later
}
else
{
// now try by CASROOT
aPluginDir = OSD_Environment ("CASROOT").Value();
if (!aPluginDir.IsEmpty())
thePluginDir = OSD_Environment ("CASROOT").Value();
if (!thePluginDir.IsEmpty())
{
aPluginDir += "/src/DrawResources";
thePluginDir += "/src/DrawResources";
aToSetCSFVariable = Standard_True; //CSF variable to be set later
}
else
{
aResult = aDirFound = Standard_False;
std::cout << FAILSTR "Neither " << aCSFVariable << ", nor CASROOT variables have been set\n";
Message::SendFail() << "Failed to load plugin: Neither " << aCSFVariable << ", nor CASROOT variables have been set";
return Standard_False;
}
}
}
if (aDirFound) {
// search directory name has been constructed, now check whether it and the file exist
TCollection_AsciiString aPluginFileName = aPluginDir + "/" + thePluginName;
OSD_File PluginFile ( aPluginFileName );
if ( PluginFile.Exists() ) {
if (aToSetCSFVariable) {
OSD_Environment aCSFVarEnv ( aCSFVariable, aPluginDir );
aCSFVarEnv.Build();
// search directory name has been constructed, now check whether it and the file exist
const TCollection_AsciiString aPluginFileName = thePluginDir + "/" + thePluginName;
OSD_File aPluginFile (aPluginFileName);
if (!aPluginFile.Exists())
{
Message::SendFail() << "Failed to load plugin: File " << aPluginFileName << " not found";
return Standard_False;
}
if (aToSetCSFVariable)
{
OSD_Environment aCSFVarEnv (aCSFVariable, thePluginDir);
aCSFVarEnv.Build();
#ifdef OCCT_DEBUG
std::cout << "Variable " << aCSFVariable.ToCString() << " has not been explicitly defined. Set to " << aPluginDir.ToCString() << std::endl;
std::cout << "Variable " << aCSFVariable << " has not been explicitly defined. Set to " << thePluginDir << std::endl;
#endif
if ( aCSFVarEnv.Failed() ) {
aResult = Standard_False;
std::cout << FAILSTR "Failed to initialize " << aCSFVariable.ToCString() << " with " << aPluginDir.ToCString() << std::endl;
}
}
} else {
aResult = Standard_False;
std::cout << FAILSTR "File " << aPluginFileName.ToCString() << " not found" << std::endl;
if (aCSFVarEnv.Failed())
{
Message::SendFail() << "Failed to load plugin: Failed to initialize " << aCSFVariable << " with " << thePluginDir;
return Standard_False;
}
}
return aResult;
return Standard_True;
}
//=======================================================================
//function : Parse
//purpose : Parse the input keys to atomic keys (<key> --> <akey>[<akey> ..])
//=======================================================================
static void Parse (Draw_MapOfAsciiString& theMap)
//! Resolve keys within input map (groups, aliases and toolkits) to the list of destination toolkits (plugins to load).
//! @param theMap [in] [out] map to resolve (will be rewritten)
//! @param theResMgr [in] resource manager to resolve keys
static void resolveKeys (Draw_MapOfAsciiString& theMap,
const Handle(Resource_Manager)& theResMgr)
{
Draw_MapOfAsciiString aMap, aMap2;
Standard_Integer j, k;
Standard_Integer aMapExtent, aMap2Extent;
aMapExtent = theMap.Extent();
for(j = 1; j <= aMapExtent; j++) {
if (!myResources.IsNull()) {
const TCollection_AsciiString& aKey = theMap.FindKey(j);
TCollection_AsciiString aResource = aKey;
if(myResources->Find(aResource.ToCString())) {
#ifdef OCCT_DEBUG
std::cout << "Parse Value ==> " << myResources->Value(aResource.ToCString()) << std::endl;
#endif
TCollection_AsciiString aValue(myResources->Value(aResource.ToCString()));
// parse aValue string
Standard_Integer i=1;
for(;;) {
TCollection_AsciiString aCurKey = aValue.Token(" \t,", i++);
#ifdef OCCT_DEBUG
std::cout << "Parse aCurKey = " << aCurKey.ToCString() << std::endl;
#endif
if(aCurKey.IsEmpty()) break;
if(!myResources->Find(aCurKey.ToCString())) {
// It is toolkit
aMap.Add(aResource);
}
else
aMap2.Add(aCurKey);
}
} else
std::cout <<"Pload : Resource = " << aResource << " is not found" << std::endl;
if(!aMap2.IsEmpty())
Parse(aMap2);
//
aMap2Extent = aMap2.Extent();
for(k = 1; k <= aMap2Extent; k++) {
aMap.Add(aMap2.FindKey(k));
}
if (theResMgr.IsNull())
{
return;
}
Draw_MapOfAsciiString aMap, aMap2;
const Standard_Integer aMapExtent = theMap.Extent();
for (Standard_Integer j = 1; j <= aMapExtent; ++j)
{
TCollection_AsciiString aValue;
const TCollection_AsciiString aResource = theMap.FindKey (j);
if (theResMgr->Find (aResource, aValue))
{
#ifdef OCCT_DEBUG
std::cout << "Parse Value ==> " << aValue << std::endl;
#endif
for (Standard_Integer aKeyIter = 1;; ++aKeyIter)
{
const TCollection_AsciiString aCurKey = aValue.Token (" \t,", aKeyIter);
#ifdef OCCT_DEBUG
std::cout << "Parse aCurKey = " << aCurKey << std::endl;
#endif
if (aCurKey.IsEmpty())
{
break;
}
if (theResMgr->Find (aCurKey.ToCString()))
{
aMap2.Add (aCurKey);
}
else
{
aMap.Add (aResource); // It is toolkit
}
}
}
else
{
Message::SendFail() << "Pload : Resource = " << aResource << " is not found";
}
if (!aMap2.IsEmpty())
{
resolveKeys (aMap2, theResMgr);
}
//
const Standard_Integer aMap2Extent = aMap2.Extent();
for (Standard_Integer k = 1; k <= aMap2Extent; ++k)
{
aMap.Add (aMap2.FindKey (k));
}
}
theMap.Assign(aMap);
theMap.Assign (aMap);
}
//=======================================================================
@@ -166,98 +170,81 @@ static void Parse (Draw_MapOfAsciiString& theMap)
//purpose :
//=======================================================================
static Standard_Integer Pload (Draw_Interpretor& di,
Standard_Integer n,
const char** argv)
static Standard_Integer Pload (Draw_Interpretor& theDI,
Standard_Integer theNbArgs,
const char** theArgVec)
{
char adef[] = "-";
TCollection_AsciiString aPluginFileName("");
TCollection_AsciiString aPluginDir(""), aPluginDir2("");
Standard_Integer aStart = 0;
Standard_Integer aFinish = n - 1;
if (n == 1) {
// Load DEFAULT key
aStart = 0;
} else {
if(argv[1][0] == adef[0]) {
aPluginFileName = argv[1];
aPluginFileName.Remove(1,1);
if (n == 2) {
// Load DEFAULT key from aPluginFileName file
aStart = 0;
aFinish = n - 2;
} else {
aStart = 2;
}
} else {
aStart = 1;
Draw_MapOfAsciiString aMap;
TCollection_AsciiString aPluginFileName;
for (Standard_Integer anArgIter = 1; anArgIter < theNbArgs; ++anArgIter)
{
const TCollection_AsciiString aTK (theArgVec[anArgIter]);
if (anArgIter == 1
&& aTK.Value (1) == '-')
{
aPluginFileName = aTK.SubString (2, aTK.Length());
}
else
{
aMap.Add (aTK);
}
}
if (aMap.IsEmpty())
{
aMap.Add ("DEFAULT"); // Load DEFAULT key
}
//if ( !FindPluginFile (aPluginFileName) ) {
if ( !FindPluginFile (aPluginFileName, aPluginDir) ) {
TCollection_AsciiString aPluginDir, aPluginDir2;
if (!findPluginFile (aPluginFileName, aPluginDir))
{
return 1;
}
}
Draw_MapOfAsciiString aMap;
TCollection_AsciiString aDEFAULT("DEFAULT");
//for(Standard_Integer i = aStart; i < n; i++)
for(Standard_Integer i = aStart; i <= aFinish; i++)
if (i == 0) {
// Load DEFAULT key
aMap.Add(aDEFAULT);
} else {
TCollection_AsciiString aTK(argv[i]);
aMap.Add(aTK);
Handle(Resource_Manager) aResMgr = new Resource_Manager (aPluginFileName.ToCString(), aPluginDir, aPluginDir2, Standard_False);
resolveKeys (aMap, aResMgr);
const Standard_Integer aMapExtent = aMap.Extent();
for (Standard_Integer aResIter = 1; aResIter <= aMapExtent; ++aResIter)
{
const TCollection_AsciiString aResource = aMap.FindKey (aResIter);
#ifdef OCCT_DEBUG
std::cout << "aResource = " << aResource << std::endl;
#endif
TCollection_AsciiString aValue;
if (!aResMgr->Find (aResource, aValue))
{
Message::SendWarning() <<"Pload : Resource = " << aResource << " is not found";
continue;
}
//myResources = new Resource_Manager(aPluginFileName.ToCString());
myResources = new Resource_Manager(aPluginFileName.ToCString(), aPluginDir, aPluginDir2, Standard_False);
Parse(aMap);
Standard_Integer j;
Standard_Integer aMapExtent;
aMapExtent = aMap.Extent();
for(j = 1; j <= aMapExtent; j++) {
const TCollection_AsciiString& aKey = aMap.FindKey(j);
TCollection_AsciiString aResource = aKey;
#ifdef OCCT_DEBUG
std::cout << "aResource = " << aResource << std::endl;
#endif
if(myResources->Find(aResource.ToCString())) {
const TCollection_AsciiString& aValue = myResources->Value(aResource.ToCString());
#ifdef OCCT_DEBUG
std::cout << "Value ==> " << aValue << std::endl;
#endif
//Draw::Load(di, aKey, aPluginFileName);
Draw::Load(di, aKey, aPluginFileName, aPluginDir, aPluginDir2, Standard_False);
#ifdef OCCT_DEBUG
std::cout << "Value ==> " << aValue << std::endl;
#endif
// Load TclScript
TCollection_AsciiString aCSFVariable ("CSF_DrawPluginTclDir");
TCollection_AsciiString aTclScriptDir;
aTclScriptDir = getenv (aCSFVariable.ToCString());
TCollection_AsciiString aTclScriptFileName;
TCollection_AsciiString aTclScriptFileNameDefaults;
aTclScriptFileName = aTclScriptDir + "/" + aValue + ".tcl";
aTclScriptFileNameDefaults = aPluginDir + "/" + aValue + ".tcl";
OSD_File aTclScriptFile ( aTclScriptFileName );
OSD_File aTclScriptFileDefaults ( aTclScriptFileNameDefaults );
if (!aTclScriptDir.IsEmpty() && aTclScriptFile.Exists()) {
#ifdef OCCT_DEBUG
std::cout << "Load " << aTclScriptFileName << " TclScript" << std::endl;
#endif
di.EvalFile( aTclScriptFileName.ToCString() );
} else if (!aPluginDir.IsEmpty() && aTclScriptFileDefaults.Exists()) {
#ifdef OCCT_DEBUG
std::cout << "Load " << aTclScriptFileNameDefaults << " TclScript" << std::endl;
#endif
di.EvalFile( aTclScriptFileNameDefaults.ToCString() );
}
} else
std::cout <<"Pload : Resource = " << aResource << " is not found" << std::endl;
Draw::Load (theDI, aResource, aPluginFileName, aPluginDir, aPluginDir2, Standard_False);
// Load TclScript
const TCollection_AsciiString aTclScriptDir = OSD_Environment ("CSF_DrawPluginTclDir").Value();
const TCollection_AsciiString aTclScriptFileName = aTclScriptDir + "/" + aValue + ".tcl";
const TCollection_AsciiString aTclScriptFileNameDefaults = aPluginDir + "/" + aValue + ".tcl";
OSD_File aTclScriptFile (aTclScriptFileName);
OSD_File aTclScriptFileDefaults (aTclScriptFileNameDefaults);
if (!aTclScriptDir.IsEmpty()
&& aTclScriptFile.Exists())
{
#ifdef OCCT_DEBUG
std::cout << "Load " << aTclScriptFileName << " TclScript" << std::endl;
#endif
theDI.EvalFile (aTclScriptFileName.ToCString());
}
else if (!aPluginDir.IsEmpty()
&& aTclScriptFileDefaults.Exists())
{
#ifdef OCCT_DEBUG
std::cout << "Load " << aTclScriptFileNameDefaults << " TclScript" << std::endl;
#endif
theDI.EvalFile (aTclScriptFileNameDefaults.ToCString());
}
}
return 0;
}