-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathOcctDrawPlugin.cpp
More file actions
136 lines (117 loc) · 3.79 KB
/
OcctDrawPlugin.cpp
File metadata and controls
136 lines (117 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <Draw.hxx>
#include <Draw_Drawable3D.hxx>
#include <Draw_Interpretor.hxx>
#include <Draw_PluginMacro.hxx>
#include <DBRep_DrawableShape.hxx>
#include <Message.hxx>
//! Class defining static method 'Factory()' for 'DPLUGIN' macros.
class OcctDrawPlugin
{
public:
DEFINE_STANDARD_ALLOC
//! Add commands to Draw_Interpretor.
static void Factory (Draw_Interpretor& theDI);
};
//! Command just printing "hello" message.
static int myhello (Draw_Interpretor& theDI,
int theNbArgs, const char** theArgVec)
{
if (theNbArgs != 1)
{
theDI << "Syntax error - wrong number of arguments";
return 1; // throw Tcl exception
}
// std::cout/std::cerr will appear in terminal,
// but will be inaccessible to Tcl
std::cout << "standard output\n";
// first argument equals to command name, e.g. "myhello"
std::cout << "command '" << theArgVec[0] << "'\n";
// output to theDI will be accessible to Tcl
theDI << "HELLO";
return 0; // normal result
}
//! A dummy drawable object.
class MyDummyDrawable : public Draw_Drawable3D
{
DEFINE_STANDARD_RTTI_INLINE(MyDummyDrawable, Draw_Drawable3D)
public:
MyDummyDrawable() {}
//! Draw object in axonometric viewer - not implemented.
virtual void DrawOn (Draw_Display& theDisp) const override { (void )theDisp; }
//! Variable dump.
virtual void Dump (Standard_OStream& theStream) const override { theStream << "MyDummyDrawable dump"; }
//! For whatis command.
virtual void Whatis (Draw_Interpretor& theDI) const override { theDI << "MyDummyDrawable"; }
};
//! Command working with drawables.
static int mydrawable (Draw_Interpretor& theDI,
int theNbArgs, const char** theArgVec)
{
Handle(Draw_Drawable3D) aDrawable;
TCollection_AsciiString aName;
for (int anArgIter = 1; anArgIter < theNbArgs; ++anArgIter)
{
TCollection_AsciiString anArg (theArgVec[anArgIter]);
anArg.LowerCase();
if (anArg == "-create"
&& aDrawable.IsNull())
{
aDrawable = new MyDummyDrawable();
}
else if (aName.IsEmpty())
{
aName = theArgVec[anArgIter];
}
else
{
theDI << "Syntax error at '" << theArgVec[anArgIter] << "'";
return 1;
}
}
if (aName.IsEmpty())
{
theDI << "Syntax error: wrong number of arguments";
return 1;
}
if (aDrawable.IsNull())
{
// get existing drawable
const char* aNameStr = aName.ToCString();
aDrawable = Draw::Get (aNameStr);
if (aDrawable.IsNull())
{
theDI << "Error: drawable '" << aName << "' not found";
return 1;
}
}
// print some basic info
theDI << "DynamicType: " << aDrawable->DynamicType()->Name();
Draw::Set (aName.ToCString(), aDrawable);
// try handling subclasses
if (Handle(MyDummyDrawable) aMyDraw = Handle(MyDummyDrawable)::DownCast (aDrawable))
{
theDI << "\nIt is my drawable!";
}
if (Handle(DBRep_DrawableShape) aDrawShape = Handle(DBRep_DrawableShape)::DownCast (aDrawable))
{
const TopoDS_Shape& aShape = aDrawShape->Shape();
theDI << "\nShapeType: " << TopAbs::ShapeTypeToString (aShape.ShapeType());
}
return 0;
}
// Add commands to Draw_Interpretor.
void OcctDrawPlugin::Factory (Draw_Interpretor& theDI)
{
// just some welcome message
Message::SendInfo() << "Loading 'occt-draw-plugin'...\n"
"Tip1: print 'help my*' to list commands defined by this plugin.\n"
"Tip2: print 'testgrid' to run tests.";
// register commands with description that could be retrieved via 'help CommandName'
const char* aGroup = "My draw commands";
theDI.Add ("myhello", "myhello - hello draw",
__FILE__, myhello, aGroup);
theDI.Add ("mydrawable", "mydrawable name [-create]",
__FILE__, mydrawable, aGroup);
}
// Implement exported function that will be called by DRAWEXE on loading plugin
DPLUGIN(OcctDrawPlugin);