forked from mcneel/rhino-developer-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmdSampleAddText.cpp
More file actions
209 lines (170 loc) · 5.68 KB
/
Copy pathcmdSampleAddText.cpp
File metadata and controls
209 lines (170 loc) · 5.68 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "stdafx.h"
CRhinoText* RhinoCreateText(
CRhinoDoc& doc,
const ON_3dPoint& pt,
const wchar_t* text,
const wchar_t* font_name,
double height,
int style,
double rotate,
ON::TextVerticalAlignment valign,
ON::TextHorizontalAlignment halign
)
{
CRhinoText* rc = nullptr;
if (!pt.IsValid() || nullptr == text || 0 == text[0])
return rc;
ON_wString face_name(font_name);
if (face_name.IsEmpty())
face_name = L"Arial";
style = RHINO_CLAMP(style, 0, 3);
const bool bBold = (0 != (style & 1));
const bool bItalic = (0 != (style & 2));
// Get a font managed by the application from the font characteristics.
const ON_Font* font = ON_Font::GetManagedFont(face_name, bBold, bItalic);
if (nullptr == font)
return rc;
height = fabs(height);
ON_Plane plane = RhinoActiveCPlane();
plane.Rotate(rotate, plane.zaxis);
// Get the current dimension style.
const ON_DimStyle& parent_dimstyle = doc.m_dimstyle_table.CurrentDimStyle();
// Create a new dimension style from properties.
ON_DimStyle dim_style = ON_DimStyle::CreateFromProperties(
parent_dimstyle,
ON::AnnotationType::Text,
font,
ON_UNSET_VALUE,
height,
doc.UnitSystem(),
valign,
halign
);
// Create a new text object
rc = doc.CreateTextObject(text, plane, pt, &dim_style);
return rc;
}
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//
// BEGIN SampleAddText command
//
#pragma region SampleAddText command
class CCommandSampleAddText : public CRhinoCommand
{
public:
CCommandSampleAddText() = default;
UUID CommandUUID() override
{
// {4C5D737C-7BB3-4152-A866-D062F7518ABA}
static const GUID SampleAddTextCommand_UUID =
{ 0x4C5D737C, 0x7BB3, 0x4152,{ 0xA8, 0x66, 0xD0, 0x62, 0xF7, 0x51, 0x8A, 0xBA } };
return SampleAddTextCommand_UUID;
}
const wchar_t* EnglishCommandName() override { return L"SampleAddText"; }
CRhinoCommand::result RunCommand(const CRhinoCommandContext& context) override;
};
// The one and only CCommandSampleAddText object
static class CCommandSampleAddText theSampleAddTextCommand;
CRhinoCommand::result CCommandSampleAddText::RunCommand(const CRhinoCommandContext& context)
{
CRhinoGetPoint gp;
gp.SetCommandPrompt(L"Text location");
gp.GetPoint();
if (gp.CommandResult() != CRhinoCommand::success)
return gp.CommandResult();
ON_3dPoint pt = gp.Point();
ON_wString text = L"Hello Rhino!";
CRhinoText* text_obj = RhinoCreateText(context.m_doc, pt, text, L"Times New Roman", 1.0, 0, 0.0, ON::TextVerticalAlignment::Bottom, ON::TextHorizontalAlignment::Left);
if (nullptr != text_obj)
{
context.m_doc.AddObject(text_obj);
context.m_doc.Redraw();
}
return CRhinoCommand::success;
}
#pragma endregion
//
// END SampleAddText command
//
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//
// BEGIN SampleAddTextObject command
//
#pragma region SampleAddTextObject command
class CCommandSampleAddTextObject : public CRhinoCommand
{
public:
CCommandSampleAddTextObject() = default;
UUID CommandUUID() override
{
// {87743F94-5612-4855-BD0C-59CD91148785}
static const GUID SampleAddTextObjectCommand_UUID =
{ 0x87743F94, 0x5612, 0x4855, { 0xBD, 0x0C, 0x59, 0xCD, 0x91, 0x14, 0x87, 0x85 } };
return SampleAddTextObjectCommand_UUID;
}
const wchar_t* EnglishCommandName() override { return L"SampleAddTextObject"; }
CRhinoCommand::result RunCommand(const CRhinoCommandContext& context) override;
};
// The one and only CCommandSampleAddTextObject object
static class CCommandSampleAddTextObject theSampleAddTextObjectCommand;
CRhinoCommand::result CCommandSampleAddTextObject::RunCommand(const CRhinoCommandContext& context)
{
CRhinoGetPoint gp;
gp.SetCommandPrompt(L"Text object location");
gp.GetPoint();
if (gp.CommandResult() != CRhinoCommand::success)
return gp.CommandResult();
CRhinoView* view = gp.View();
if (nullptr == view)
return CRhinoCommand::failure;
ON_3dPoint pt = gp.Point();
ON_wString text = L"Hello Rhino!";
CRhinoText* text_obj = RhinoCreateText(context.m_doc, pt, text, L"Times New Roman", 1.0, 0, 0.0, ON::TextVerticalAlignment::Bottom, ON::TextHorizontalAlignment::Left);
if (nullptr == text_obj)
return CRhinoCommand::failure;
const ON_Annotation* annotation = text_obj->Annotation();
if (nullptr == annotation)
return CRhinoCommand::failure;
const ON_DimStyle* dim_style = &annotation->DimensionStyle(
context.m_doc.m_dimstyle_table[context.m_doc.m_dimstyle_table.FindDimStyleFromId(annotation->DimensionStyleId(),
true,
true,
context.m_doc.m_dimstyle_table.CurrentDimStyleIndex())]);
ON_SimpleArray<ON_Object*> output;
bool rc = RhinoCreateTextObjectGeometry(
annotation,
&view->Viewport().VP(),
dim_style,
1.0,
true,
context.m_doc.AbsoluteTolerance(),
1.0,
ON::object_type::extrusion_object,
1.0,
0.0,
output
);
if (!rc)
return CRhinoCommand::failure;
for (int i = 0; i < output.Count(); i++)
{
if (nullptr != output[i])
{
CRhinoExtrusionObject* extrusion_obj = new CRhinoExtrusionObject();
extrusion_obj->SetExtrusion(ON_Extrusion::Cast(output[i]));
context.m_doc.AddObject(extrusion_obj);
}
}
context.m_doc.Redraw();
return CRhinoCommand::success;
}
#pragma endregion
//
// END SampleAddTextObject command
//
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////