-
Notifications
You must be signed in to change notification settings - Fork 93
/
ExtractInfo.cpp
271 lines (221 loc) · 9.63 KB
/
ExtractInfo.cpp
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*++
Copyright (C) 2019 3MF Consortium
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL MICROSOFT AND/OR NETFABB BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Abstract:
ExtractInfo.cpp : 3MF Read Example
--*/
#include <iostream>
#include <string>
#include <algorithm>
#include "lib3mf_dynamic.hpp"
using namespace Lib3MF;
void printVersion(PWrapper wrapper) {
Lib3MF_uint32 nMajor, nMinor, nMicro;
wrapper->GetLibraryVersion(nMajor, nMinor, nMicro);
std::cout << "lib3mf version = " << nMajor << "." << nMinor << "." << nMicro;
std::string sReleaseInfo, sBuildInfo;
if (wrapper->GetPrereleaseInformation(sReleaseInfo)) {
std::cout << "-" << sReleaseInfo;
}
if (wrapper->GetBuildInformation(sBuildInfo)) {
std::cout << "+" << sBuildInfo;
}
std::cout << std::endl;
}
void ShowThumbnailInformation(PModel model)
{
/*
// TODO: this is not yet implemented in lib3mf
*/
}
void ShowMetaDataInformation(PMetaDataGroup metaDataGroup)
{
Lib3MF_uint32 nMetaDataCount = metaDataGroup->GetMetaDataCount();
for (Lib3MF_uint32 iMeta = 0; iMeta < nMetaDataCount; iMeta++) {
PMetaData metaData = metaDataGroup->GetMetaData(iMeta);
std::string sMetaDataValue = metaData->GetValue();
std::string sMetaDataName = metaData->GetName();
std::cout << "Metadatum: " << iMeta << ":" << std::endl;
std::cout << "Name = \"" << sMetaDataName << "\"" << std::endl;
std::cout << "Value = \"" << sMetaDataValue << "\"" << std::endl;
}
}
void ShowSliceStack(PSliceStack sliceStack, std::string indent)
{
std::cout << indent << "SliceStackID: " << sliceStack->GetResourceID() << std::endl;
if (sliceStack->GetSliceCount() > 0) {
std::cout << indent << " Slice count: " << sliceStack->GetSliceCount() << std::endl;
}
if (sliceStack->GetSliceRefCount() > 0) {
std::cout << indent << " Slice ref count: " << sliceStack->GetSliceRefCount() << std::endl;
for (Lib3MF_uint64 iSliceRef = 0; iSliceRef < sliceStack->GetSliceRefCount(); iSliceRef++) {
std::cout << indent << " Slice ref : " << sliceStack->GetSliceStackReference(iSliceRef)->GetResourceID() << std::endl;
}
}
}
void ShowObjectProperties(PObject object)
{
std::cout << " Name: \"" << object->GetName() << "\"" << std::endl;
std::cout << " PartNumber: \"" << object->GetPartNumber() << "\"" << std::endl;
switch (object->GetType()) {
case eObjectType::Model:
std::cout << " Object type: model" << std::endl;
break;
case eObjectType::Support:
std::cout << " Object type: support" << std::endl;
break;
case eObjectType::SolidSupport:
std::cout << " Object type: solidsupport" << std::endl;
break;
case eObjectType::Other:
std::cout << " Object type: other" << std::endl;
break;
default:
std::cout << " Object type: invalid" << std::endl;
break;
}
if (object->HasSlices(false)) {
PSliceStack sliceStack = object->GetSliceStack();
ShowSliceStack(sliceStack, " ");
}
if (object->GetMetaDataGroup()->GetMetaDataCount() > 0) {
ShowMetaDataInformation(object->GetMetaDataGroup());
}
}
void ShowMeshObjectInformation(PMeshObject meshObject)
{
std::cout << "mesh object #" << meshObject->GetResourceID() << ": " << std::endl;
ShowObjectProperties(meshObject);
Lib3MF_uint64 nVertexCount = meshObject->GetVertexCount();
Lib3MF_uint64 nTriangleCount = meshObject->GetTriangleCount();
PBeamLattice beamLattice = meshObject->BeamLattice();
// Output data
std::cout << " Vertex count: " << nVertexCount << std::endl;
std::cout << " Triangle count: " << nTriangleCount << std::endl;
Lib3MF_uint64 nBeamCount = beamLattice->GetBeamCount();
if (nBeamCount > 0) {
std::cout << " Beam count: " << nBeamCount << std::endl;
Lib3MF_uint32 nRepresentationMesh;
if (beamLattice->GetRepresentation(nRepresentationMesh))
std::cout << " |_Representation Mesh ID: " << nRepresentationMesh << std::endl;
eLib3MFBeamLatticeClipMode eClipMode;
Lib3MF_uint32 nClippingMesh;
beamLattice->GetClipping(eClipMode, nClippingMesh);
if (eClipMode != eBeamLatticeClipMode::NoClipMode)
std::cout << " |_Clipping Mesh ID: " << nClippingMesh << "(mode=" << (int)eClipMode << ")" << std::endl;
if (beamLattice->GetBeamSetCount() > 0) {
std::cout << " |_BeamSet count: " << beamLattice->GetBeamSetCount() << std::endl;
}
}
}
void ShowTransform(sLib3MFTransform transform, std::string indent) {
std::cout << indent << "Transformation: [ " << transform.m_Fields[0][0] << " " << transform.m_Fields[1][0] << " " << transform.m_Fields[2][0] << " " << transform.m_Fields[3][0] << " ]" << std::endl;
std::cout << indent << " [ " << transform.m_Fields[0][1] << " " << transform.m_Fields[1][1] << " " << transform.m_Fields[2][1] << " " << transform.m_Fields[3][1] << " ]" << std::endl;
std::cout << indent << " [ " << transform.m_Fields[0][2] << " " << transform.m_Fields[1][2] << " " << transform.m_Fields[2][2] << " " << transform.m_Fields[3][2] << " ]" << std::endl;
}
void ShowComponentsObjectInformation(PComponentsObject componentsObject)
{
std::cout << "components object #" << componentsObject->GetResourceID() << ": " << std::endl;
ShowObjectProperties(componentsObject);
std::cout << " Component count: " << componentsObject->GetComponentCount() << std::endl;
for (Lib3MF_uint32 nIndex = 0; nIndex < componentsObject->GetComponentCount(); nIndex++) {
PComponent component = componentsObject->GetComponent(nIndex);
std::cout << " Component " << nIndex << ": Object ID: " << component->GetObjectResourceID() << std::endl;
if (component->HasTransform()) {
ShowTransform(component->GetTransform(), " ");
}
else {
std::cout << " Transformation: none" << std::endl;
}
}
}
void ExtractInfoExample(std::string sFileName) {
std::cout << "------------------------------------------------------------------" << std::endl;
std::cout << "3MF Read example" << std::endl;
std::string libpath = ("."); // TODO: put the location of the Lib3MF-library file here.
auto wrapper = Lib3MF::CWrapper::loadLibrary(libpath + "/lib3mf" + LSUFFIXOUT); // TODO: add correct suffix of the library
printVersion(wrapper);
std::cout << "------------------------------------------------------------------" << std::endl;
PModel model = wrapper->CreateModel();
// Import Model from 3MF File
{
PReader reader = model->QueryReader("3mf");
// And deactivate the strict mode (default is "false", anyway. This just demonstrates where/how to use it).
reader->SetStrictModeActive(false);
reader->ReadFromFile(sFileName);
for (Lib3MF_uint32 iWarning = 0; iWarning < reader->GetWarningCount(); iWarning++) {
Lib3MF_uint32 nErrorCode;
std::string sWarningMessage = reader->GetWarning(iWarning, nErrorCode);
std::cout << "Encountered warning #" << nErrorCode << " : " << sWarningMessage << std::endl;
}
}
ShowThumbnailInformation(model);
ShowMetaDataInformation(model->GetMetaDataGroup());
PSliceStackIterator sliceStacks = model->GetSliceStacks();
while (sliceStacks->MoveNext()) {
PSliceStack sliceStack = sliceStacks->GetCurrentSliceStack();
ShowSliceStack(sliceStack, "");
}
PObjectIterator objectIterator = model->GetObjects();
while (objectIterator->MoveNext()) {
PObject object = objectIterator->GetCurrentObject();
if (object->IsMeshObject()) {
ShowMeshObjectInformation(model->GetMeshObjectByID(object->GetResourceID()));
}
else if (object->IsComponentsObject()) {
ShowComponentsObjectInformation(model->GetComponentsObjectByID(object->GetResourceID()));
}
else {
std::cout << "unknown object #" << object->GetResourceID() << ": " << std::endl;
}
}
PBuildItemIterator buildItemIterator = model->GetBuildItems();
while (buildItemIterator->MoveNext()) {
PBuildItem buildItem = buildItemIterator->GetCurrent();
std::cout << "Build item (Object #" << buildItem->GetObjectResourceID() << "): " << std::endl;
if (buildItem->HasObjectTransform()) {
ShowTransform(buildItem->GetObjectTransform(), " ");
}
else {
std::cout << " Transformation: none" << std::endl;
}
std::cout << " Part number: \"" << buildItem->GetPartNumber() << "\"" << std::endl;
if (buildItem->GetMetaDataGroup()->GetMetaDataCount() > 0) {
ShowMetaDataInformation(buildItem->GetMetaDataGroup());
}
}
std::cout << "done" << std::endl;
}
int main(int argc, char** argv) {
// Parse Arguments
if (argc != 2) {
std::cout << "Usage: " << std::endl;
std::cout << "ExtractInfo.exe model.3mf" << std::endl;
return 0;
}
try {
ExtractInfoExample(argv[1]);
}
catch (ELib3MFException &e) {
std::cout << e.what() << std::endl;
return e.getErrorCode();
}
return 0;
}