Debian Codegraph @

ogr/ogrsf_frmts/dwg/ogrdwgdatasource.cpp

raw
1 /******************************************************************************
2 *
3 * Project: DWG Translator
4 * Purpose: Implements OGRDWGDataSource class
5 * Author: Frank Warmerdam, [email protected]
6 *
7 ******************************************************************************
8 * Copyright (c) 2011, Frank Warmerdam <[email protected]>
9 *
10 * SPDX-License-Identifier: MIT
11 ****************************************************************************/
12
13 #include "ogr_dwg.h"
14 #include "cpl_conv.h"
15 #include "cpl_string.h"
16
17 /************************************************************************/
18 /* OGRDWGDataSource() */
19 /************************************************************************/
20
21 OGRDWGDataSource::OGRDWGDataSource()
22 : bInlineBlocks(FALSE), bAttributes(FALSE), bAllAttributes(TRUE),
23 poServices(nullptr), poDb(static_cast<const OdRxObject *>(nullptr))
24
25 {
26 }
27
28 /************************************************************************/
29 /* ~OGRDWGDataSource() */
30 /************************************************************************/
31
32 OGRDWGDataSource::~OGRDWGDataSource()
33
34 {
35 /* -------------------------------------------------------------------- */
36 /* Destroy layers. */
37 /* -------------------------------------------------------------------- */
38 while (!apoLayers.empty())
39 {
40 delete apoLayers.back();
41 apoLayers.pop_back();
42 }
43 }
44
45 /************************************************************************/
46 /* GetLayer() */
47 /************************************************************************/
48
49 const OGRLayer *OGRDWGDataSource::GetLayer(int iLayer) const
50
51 {
52 if (iLayer < 0 || iLayer >= (int)apoLayers.size())
53 return nullptr;
54 else
55 return apoLayers[iLayer];
56 }
57
58 /************************************************************************/
59 /* Open() */
60 /************************************************************************/
61
62 int OGRDWGDataSource::Open(OGRDWGServices *poServicesIn,
63 const char *pszFilename, int /*bHeaderOnly*/)
64
65 {
66 poServices = poServicesIn;
67
68 osEncoding = CPL_ENC_ISO8859_1;
69
70 bInlineBlocks =
71 CPLTestBool(CPLGetConfigOption("DWG_INLINE_BLOCKS", "TRUE"));
72 bAttributes = CPLTestBool(CPLGetConfigOption("DWG_ATTRIBUTES", "FALSE"));
73 bAllAttributes =
74 CPLTestBool(CPLGetConfigOption("DWG_ALL_ATTRIBUTES", "TRUE"));
75 m_bClosedLineAsPolygon =
76 CPLTestBool(CPLGetConfigOption("DWG_CLOSED_LINE_AS_POLYGON", "FALSE"));
77
78 /* -------------------------------------------------------------------- */
79 /* Open the file. */
80 /* -------------------------------------------------------------------- */
81 try
82 {
83 OdString f(pszFilename);
84 poDb = poServices->readFile(f.c_str(), true, false, Oda::kShareDenyNo);
85 }
86 catch (OdError &e)
87 {
88 CPLError(CE_Failure, CPLE_AppDefined, "%s",
89 (const char *)poServices->getErrorDescription(e.code()));
90 }
91 catch (...)
92 {
93 CPLError(CE_Failure, CPLE_AppDefined,
94 "DWG readFile(%s) failed with generic exception.",
95 pszFilename);
96 }
97
98 if (poDb.isNull())
99 return FALSE;
100
101 /* -------------------------------------------------------------------- */
102 /* Process the header, picking up a few useful pieces of */
103 /* information. */
104 /* -------------------------------------------------------------------- */
105 ReadHeaderSection();
106 ReadLineTypeDefinitions();
107 ReadLayerDefinitions();
108
109 /* -------------------------------------------------------------------- */
110 /* Create a blocks layer if we are not in inlining mode. */
111 /* -------------------------------------------------------------------- */
112 if (!bInlineBlocks)
113 apoLayers.push_back(new OGRDWGBlocksLayer(this));
114
115 /* -------------------------------------------------------------------- */
116 /* Create out layer object - we will need it when interpreting */
117 /* blocks. */
118 /* -------------------------------------------------------------------- */
119 if (bAttributes)
120 {
121 ReadAttDefinitions();
122 }
123 apoLayers.push_back(new OGRDWGLayer(this));
124
125 ReadBlocksSection();
126
127 return TRUE;
128 }
129
130 /************************************************************************/
131 /* ReadAttDefinitions() */
132 /************************************************************************/
133
134 void OGRDWGDataSource::ReadAttDefinitions()
135
136 {
137 OdDbBlockTablePtr pTable = poDb->getBlockTableId().safeOpenObject();
138 OdDbSymbolTableIteratorPtr pBlkIter = pTable->newIterator();
139 OdDbBlockTableRecordPtr poModelSpace, poBlock;
140
141 for (pBlkIter->start(); !pBlkIter->done(); pBlkIter->step())
142 {
143 poBlock = pBlkIter->getRecordId().safeOpenObject();
144 OdDbObjectIteratorPtr poEntIter = poBlock->newIterator();
145
146 CPLString *attName = nullptr;
147 /* --------------------------------------------------------------------
148 */
149 /* Fetch the next attribute definition. */
150 /* --------------------------------------------------------------------
151 */
152 for (poEntIter->start(); !poEntIter->done(); poEntIter->step())
153 {
154 OdDbObjectId oId = poEntIter->objectId();
155 OdDbEntityPtr poEntity = OdDbEntity::cast(oId.openObject());
156 if (poEntity.isNull())
157 continue;
158 /* --------------------------------------------------------------------
159 */
160 /* Check AcDbAttributeDefinition then create and insert
161 * attribute */
162 /* --------------------------------------------------------------------
163 */
164 OdRxClass *poClass = poEntity->isA();
165 const OdString osName = poClass->name();
166 const char *pszEntityClassName = osName;
167 if (EQUAL(pszEntityClassName, "AcDbAttributeDefinition"))
168 {
169 OdDbAttributeDefinitionPtr poAttributeDefinition =
170 OdDbAttributeDefinition::cast(poEntity);
171 if (bAllAttributes || !poAttributeDefinition->isInvisible())
172 {
173 const char *tagName = poAttributeDefinition->tag();
174 attName = new CPLString(tagName);
175 if (attributeFields.count(*attName) == 0)
176 {
177 attributeFields.insert(*attName);
178 }
179 }
180 }
181 }
182 }
183 }
184
185 /************************************************************************/
186 /* ReadLayerDefinitions() */
187 /************************************************************************/
188
189 void OGRDWGDataSource::ReadLayerDefinitions()
190
191 {
192 OdDbLayerTablePtr poLT = poDb->getLayerTableId().safeOpenObject();
193 OdDbSymbolTableIteratorPtr poIter = poLT->newIterator();
194
195 for (poIter->start(); !poIter->done(); poIter->step())
196 {
197 CPLString osValue;
198 OdDbLayerTableRecordPtr poLD = poIter->getRecordId().safeOpenObject();
199 std::map<CPLString, CPLString> oLayerProperties;
200
201 CPLString osLayerName =
202 ACTextUnescape(poLD->getName(), GetEncoding(), false);
203
204 oLayerProperties["Exists"] = "1";
205
206 OdDbLinetypeTableRecordPtr poLinetypeTableRecord =
207 poLD->linetypeObjectId().safeOpenObject();
208 oLayerProperties["Linetype"] = ACTextUnescape(
209 poLinetypeTableRecord->getName(), GetEncoding(), false);
210
211 osValue.Printf("%d", poLD->colorIndex());
212 oLayerProperties["Color"] = osValue;
213
214 osValue.Printf("%d", (int)poLD->lineWeight());
215 oLayerProperties["LineWeight"] = osValue;
216
217 if (poLD->isFrozen() || poLD->isHidden() || poLD->isOff())
218 oLayerProperties["Hidden"] = "1";
219 else
220 oLayerProperties["Hidden"] = "0";
221
222 oLayerTable[osLayerName] = oLayerProperties;
223 }
224
225 CPLDebug("DWG", "Read %d layer definitions.", (int)oLayerTable.size());
226 }
227
228 /************************************************************************/
229 /* LookupLayerProperty() */
230 /************************************************************************/
231
232 const char *OGRDWGDataSource::LookupLayerProperty(const char *pszLayer,
233 const char *pszProperty)
234
235 {
236 if (pszLayer == nullptr)
237 return nullptr;
238
239 try
240 {
241 return (oLayerTable[pszLayer])[pszProperty];
242 }
243 catch (...)
244 {
245 return nullptr;
246 }
247 }
248
249 /************************************************************************/
250 /* ReadLineTypeDefinitions() */
251 /************************************************************************/
252
253 void OGRDWGDataSource::ReadLineTypeDefinitions()
254
255 {
256 OdDbLinetypeTablePtr poTable = poDb->getLinetypeTableId().safeOpenObject();
257 OdDbSymbolTableIteratorPtr poIter = poTable->newIterator();
258
259 for (poIter->start(); !poIter->done(); poIter->step())
260 {
261 CPLString osLineTypeName;
262 CPLString osLineTypeDef;
263 OdDbLinetypeTableRecordPtr poLT =
264 poIter->getRecordId().safeOpenObject();
265
266 osLineTypeName = ACTextUnescape(poLT->getName(), GetEncoding(), false);
267
268 if (poLT->numDashes())
269 {
270 for (int i = 0; i < poLT->numDashes(); i++)
271 {
272 if (i > 0)
273 osLineTypeDef += " ";
274
275 CPLString osValue;
276 osValue.Printf("%g", fabs(poLT->dashLengthAt(i)));
277 osLineTypeDef += osValue;
278
279 osLineTypeDef += "g";
280 }
281
282 oLineTypeTable[osLineTypeName] = osLineTypeDef;
283 CPLDebug("DWG", "LineType '%s' = '%s'", osLineTypeName.c_str(),
284 osLineTypeDef.c_str());
285 }
286 }
287 }
288
289 /************************************************************************/
290 /* LookupLineType() */
291 /************************************************************************/
292
293 const char *OGRDWGDataSource::LookupLineType(const char *pszName)
294
295 {
296 if (oLineTypeTable.count(pszName) > 0)
297 return oLineTypeTable[pszName];
298 else
299 return nullptr;
300 }
301
302 /************************************************************************/
303 /* ReadHeaderSection() */
304 /************************************************************************/
305
306 void OGRDWGDataSource::ReadHeaderSection()
307
308 {
309 // using: DWGCODEPAGE, DIMTXT, LUPREC
310
311 CPLString osValue;
312
313 osValue.Printf("%d", poDb->getLUPREC());
314 oHeaderVariables["$LUPREC"] = osValue;
315
316 osValue.Printf("%g", poDb->dimtxt());
317 oHeaderVariables["$DIMTXT"] = osValue;
318
319 CPLDebug("DWG", "Read %d header variables.", (int)oHeaderVariables.size());
320
321 /* -------------------------------------------------------------------- */
322 /* Decide on what CPLRecode() name to use for the files */
323 /* encoding or allow the encoding to be overridden. */
324 /* -------------------------------------------------------------------- */
325 CPLString osCodepage = GetVariable("$DWGCODEPAGE", "ANSI_1252");
326
327 // not strictly accurate but works even without iconv.
328 if (osCodepage == "ANSI_1252")
329 osEncoding = CPL_ENC_ISO8859_1;
330 else if (STARTS_WITH_CI(osCodepage, "ANSI_"))
331 {
332 osEncoding = "CP";
333 osEncoding += osCodepage + 5;
334 }
335 else
336 {
337 // fallback to the default
338 osEncoding = CPL_ENC_ISO8859_1;
339 }
340
341 if (CPLGetConfigOption("DWG_ENCODING", nullptr) != nullptr)
342 osEncoding = CPLGetConfigOption("DWG_ENCODING", nullptr);
343
344 if (osEncoding != CPL_ENC_ISO8859_1)
345 CPLDebug("DWG", "Treating DWG as encoding '%s', $DWGCODEPAGE='%s'",
346 osEncoding.c_str(), osCodepage.c_str());
347 }
348
349 /************************************************************************/
350 /* GetVariable() */
351 /* */
352 /* Fetch a variable that came from the HEADER section. */
353 /************************************************************************/
354
355 const char *OGRDWGDataSource::GetVariable(const char *pszName,
356 const char *pszDefault)
357
358 {
359 if (oHeaderVariables.count(pszName) == 0)
360 return pszDefault;
361 else
362 return oHeaderVariables[pszName];
363 }
364
365 /************************************************************************/
366 /* AddStandardFields() */
367 /************************************************************************/
368
369 void OGRDWGDataSource::AddStandardFields(OGRFeatureDefn *poFeatureDefn)
370
371 {
372 OGRFieldDefn oLayerField("Layer", OFTString);
373 poFeatureDefn->AddFieldDefn(&oLayerField);
374
375 OGRFieldDefn oClassField("SubClasses", OFTString);
376 poFeatureDefn->AddFieldDefn(&oClassField);
377
378 OGRFieldDefn oExtendedField("ExtendedEntity", OFTString);
379 poFeatureDefn->AddFieldDefn(&oExtendedField);
380
381 OGRFieldDefn oLinetypeField("Linetype", OFTString);
382 poFeatureDefn->AddFieldDefn(&oLinetypeField);
383
384 OGRFieldDefn oEntityHandleField("EntityHandle", OFTString);
385 poFeatureDefn->AddFieldDefn(&oEntityHandleField);
386
387 OGRFieldDefn oTextField("Text", OFTString);
388 poFeatureDefn->AddFieldDefn(&oTextField);
389
390 if (!bInlineBlocks)
391 {
392 OGRFieldDefn oBlockNameField("BlockName", OFTString);
393 poFeatureDefn->AddFieldDefn(&oBlockNameField);
394
395 OGRFieldDefn oBlockAttrField("BlockAttributes", OFTString);
396 oBlockAttrField.SetSubType(OFSTJSON);
397 poFeatureDefn->AddFieldDefn(&oBlockAttrField);
398 }
399 }