| 1 | /******************************************************************************* |
| 2 | * Project: libopencad |
| 3 | * Purpose: OpenSource CAD formats support library |
| 4 | * Author: Alexandr Borzykh, mush3d at gmail.com |
| 5 | * Author: Dmitry Baryshnikov, [email protected] |
| 6 | * Language: C++ |
| 7 | ******************************************************************************* |
| 8 | * The MIT License (MIT) |
| 9 | * |
| 10 | * Copyright (c) 2016 Alexandr Borzykh |
| 11 | * Copyright (c) 2016-2018 NextGIS, <[email protected]> |
| 12 | * |
| 13 | * SPDX-License-Identifier: MIT |
| 14 | *******************************************************************************/ |
| 15 | #include "cadtables.h"cxx . . $ `<file>/ogr/ogrsf_frmts/cad/libopencad/cadtables.h`/ |
| 16 | #include "opencad_api.h"cxx . . $ `<file>/ogr/ogrsf_frmts/cad/libopencad/opencad_api.h`/ |
| 17 | |
| 18 | #include <cassert> |
| 19 | #include <iostream> |
| 20 | #include <memory> |
| 21 | #include <set> |
| 22 | |
| 23 | using namespace std; |
| 24 | |
| 25 | CADTablescxx . . $ CADTables#::CADTablescxx . . $ CADTables#CADTables(49f6e7a06ebc5aa8).() |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | void CADTablescxx . . $ CADTables#::AddTablecxx . . $ CADTables#AddTable(8b895ec3e0a6269f).( TableTypecxx . . $ CADTables#TableType# eType, const CADHandlecxx . . $ CADHandle#& hHandle ) |
| 30 | { |
| 31 | mapTablescxx . . $ CADTables#mapTables.[cxx . . $ std/map#`operator[]`(bbd415b616967878).eType] =cxx . . $ CADHandle#`operator=`(30d661831aedc41c). hHandle; |
| 32 | } |
| 33 | |
| 34 | int CADTablescxx . . $ CADTables#::ReadTablecxx . . $ CADTables#ReadTable(8d67ceaa56669c44).( CADFilecxx . . $ CADFile# * const pCADFile, CADTablescxx . . $ CADTables#::TableTypecxx . . $ CADTables#TableType# eType ) |
| 35 | { |
| 36 | auto iterAskedTable = mapTablescxx . . $ CADTables#mapTables..findcxx . . $ std/map#find(36f8d56706583706).( eType ); |
| 37 | if( iterAskedTable ==cxx . . $ std/`operator==`(be6a71a859368080). mapTablescxx . . $ CADTables#mapTables..endcxx . . $ std/map#end(7e4de33083b6e738).() ) |
| 38 | return CADErrorCodescxx . . $ CADErrorCodes#::TABLE_READ_FAILEDcxx . . $ TABLE_READ_FAILED.; |
| 39 | |
| 40 | // TODO: read different tables |
| 41 | switch( eType ) |
| 42 | { |
| 43 | case LayersTablecxx . . $ CADTables#LayersTable.: |
| 44 | return ReadLayersTablecxx . . $ CADTables#ReadLayersTable(8b943d6015c807fc).( pCADFile, iterAskedTable->cxx . . $ std/_Rb_tree_iterator#`operator->`(34d6acfd341ba7ab).secondcxx . . $ std/pair#second..getAsLongcxx . . $ CADHandle#getAsLong(57a65ae535d7db84).() ); |
| 45 | default: |
| 46 | stdcxx . . $ std/::cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Unsupported table."; |
| 47 | break; |
| 48 | } |
| 49 | |
| 50 | return CADErrorCodescxx . . $ CADErrorCodes#::SUCCESScxx . . $ SUCCESS.; |
| 51 | } |
| 52 | |
| 53 | size_t CADTablescxx . . $ CADTables#::GetLayerCountcxx . . $ CADTables#GetLayerCount(fe4d2d262d4164a8).() const |
| 54 | { |
| 55 | return aLayerscxx . . $ CADTables#aLayers..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).(); |
| 56 | } |
| 57 | |
| 58 | CADLayercxx . . $ CADLayer#& CADTablescxx . . $ CADTables#::GetLayercxx . . $ CADTables#GetLayer(cdcac8ab8ba6f12).( size_t iIndex ) |
| 59 | { |
| 60 | return aLayerscxx . . $ CADTables#aLayers.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).iIndex]; |
| 61 | } |
| 62 | |
| 63 | CADHandlecxx . . $ CADHandle# CADTablescxx . . $ CADTables#::GetTableHandlecxx . . $ CADTables#GetTableHandle(d5b925eebcd6d15c).( enum TableTypecxx . . $ CADTables#TableType# eType ) |
| 64 | { |
| 65 | // FIXME: need to add try/catch to prevent crashes on not found elem. |
| 66 | return mapTablescxx . . $ CADHandle#CADHandle(68fc48aae2612a1c).[cxx . . $ std/map#`operator[]`(bbd415b616967878).eType]; |
| 67 | } |
| 68 | |
| 69 | int CADTablescxx . . $ CADTables#::ReadLayersTablecxx . . $ CADTables#ReadLayersTable(8b943d6015c807fc).( CADFilecxx . . $ CADFile# * const pCADFile, long dLayerControlHandle ) |
| 70 | { |
| 71 | // Reading Layer Control obj, and aLayers. |
| 72 | unique_ptrcxx . . $ std/unique_ptr#<CADObjectcxx . . $ CADObject#> pCADObjectcxx . . $ std/unique_ptr#unique_ptr(c8c3025d73122d7d).( pCADFile->GetObjectcxx . . $ CADFile#GetObject(782acc3ee44d2069).( dLayerControlHandle ) ); |
| 73 | |
| 74 | CADLayerControlObjectcxx . . $ CADLayerControlObject#* spLayerControl = |
| 75 | dynamic_cast<CADLayerControlObjectcxx . . $ CADLayerControlObject# *>(pCADObject.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).()); |
| 76 | if( !spLayerControl ) |
| 77 | { |
| 78 | return CADErrorCodescxx . . $ CADErrorCodes#::TABLE_READ_FAILEDcxx . . $ TABLE_READ_FAILED.; |
| 79 | } |
| 80 | |
| 81 | for( size_t i = 0; i < spLayerControl->hLayerscxx . . $ CADLayerControlObject#hLayers..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).(); ++i ) |
| 82 | { |
| 83 | if( !spLayerControl->hLayerscxx . . $ CADLayerControlObject#hLayers.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i].isNullcxx . . $ CADHandle#isNull(50ce9a9e25b4a850).() ) |
| 84 | { |
| 85 | CADLayercxx . . $ CADLayer# oCADLayercxx . . $ CADLayer#CADLayer(7f50ecc043c46aa3).( pCADFile ); |
| 86 | |
| 87 | // Init CADLayer from CADLayerObject properties |
| 88 | CADObjectcxx . . $ CADObject#* pCADLayerObject = pCADFile->GetObjectcxx . . $ CADFile#GetObject(782acc3ee44d2069).( |
| 89 | spLayerControl->hLayerscxx . . $ CADLayerControlObject#hLayers.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i].getAsLongcxx . . $ CADHandle#getAsLong(57a65ae535d7db84).() ); |
| 90 | unique_ptrcxx . . $ std/unique_ptr#<CADLayerObjectcxx . . $ CADLayerObject#> oCADLayerObjcxx . . $ std/unique_ptr#unique_ptr(c8c3025d73122d7d).( |
| 91 | dynamic_cast<CADLayerObjectcxx . . $ CADLayerObject# *>( pCADLayerObject ) ); |
| 92 | |
| 93 | if(oCADLayerObj) |
| 94 | { |
| 95 | oCADLayer.setNamecxx . . $ CADLayer#setName(9c546c94e5fcce4e).( oCADLayerObj->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).sLayerNamecxx . . $ CADLayerObject#sLayerName. ); |
| 96 | oCADLayer.setFrozencxx . . $ CADLayer#setFrozen(b45ddcbc8423892c).( oCADLayerObj->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).bFrozencxx . . $ CADLayerObject#bFrozen. ); |
| 97 | oCADLayer.setOncxx . . $ CADLayer#setOn(b45ddcbc8423892c).( oCADLayerObj->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).bOncxx . . $ CADLayerObject#bOn. ); |
| 98 | oCADLayer.setFrozenByDefaultcxx . . $ CADLayer#setFrozenByDefault(b45ddcbc8423892c).( oCADLayerObj->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).bFrozenInNewVPORTcxx . . $ CADLayerObject#bFrozenInNewVPORT. ); |
| 99 | oCADLayer.setLockedcxx . . $ CADLayer#setLocked(b45ddcbc8423892c).( oCADLayerObj->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).bLockedcxx . . $ CADLayerObject#bLocked. ); |
| 100 | oCADLayer.setLineWeightcxx . . $ CADLayer#setLineWeight(68e3ae50fe6578a9).( oCADLayerObj->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).dLineWeightcxx . . $ CADLayerObject#dLineWeight. ); |
| 101 | oCADLayer.setColorcxx . . $ CADLayer#setColor(68e3ae50fe6578a9).( oCADLayerObj->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).dCMColorcxx . . $ CADLayerObject#dCMColor. ); |
| 102 | oCADLayer.setIdcxx . . $ CADLayer#setId(ae19885fdae2095).( aLayerscxx . . $ CADTables#aLayers..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() + 1 ); |
| 103 | oCADLayer.setHandlecxx . . $ CADLayer#setHandle(1624ee8275b10bda).( oCADLayerObj->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).hObjectHandlecxx . . $ CADBaseControlObject#hObjectHandle..getAsLongcxx . . $ CADHandle#getAsLong(57a65ae535d7db84).() ); |
| 104 | |
| 105 | aLayerscxx . . $ CADTables#aLayers..push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).( stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(oCADLayer) ); |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | delete pCADLayerObject; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | auto iterBlockMS = mapTablescxx . . $ CADTables#mapTables..findcxx . . $ std/map#find(36f8d56706583706).( BlockRecordModelSpacecxx . . $ CADTables#BlockRecordModelSpace. ); |
| 115 | if( iterBlockMS ==cxx . . $ std/`operator==`(be6a71a859368080). mapTablescxx . . $ CADTables#mapTables..endcxx . . $ std/map#end(7e4de33083b6e738).() ) |
| 116 | return CADErrorCodescxx . . $ CADErrorCodes#::TABLE_READ_FAILEDcxx . . $ TABLE_READ_FAILED.; |
| 117 | |
| 118 | CADObjectcxx . . $ CADObject#* pCADBlockObject = pCADFile->GetObjectcxx . . $ CADFile#GetObject(782acc3ee44d2069).( |
| 119 | iterBlockMS->cxx . . $ std/_Rb_tree_iterator#`operator->`(34d6acfd341ba7ab).secondcxx . . $ std/pair#second..getAsLongcxx . . $ CADHandle#getAsLong(57a65ae535d7db84).() ); |
| 120 | unique_ptrcxx . . $ std/unique_ptr#<CADBlockHeaderObjectcxx . . $ CADBlockHeaderObject#> spModelSpacecxx . . $ std/unique_ptr#unique_ptr(c8c3025d73122d7d).( |
| 121 | dynamic_cast<CADBlockHeaderObjectcxx . . $ CADBlockHeaderObject# *>( pCADBlockObject ) ); |
| 122 | if(!spModelSpace) |
| 123 | { |
| 124 | delete pCADBlockObject; |
| 125 | return CADErrorCodescxx . . $ CADErrorCodes#::TABLE_READ_FAILEDcxx . . $ TABLE_READ_FAILED.; |
| 126 | } |
| 127 | |
| 128 | if(spModelSpace->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).hEntitiescxx . . $ CADBlockHeaderObject#hEntities..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).() < 2) |
| 129 | { |
| 130 | return CADErrorCodescxx . . $ CADErrorCodes#::TABLE_READ_FAILEDcxx . . $ TABLE_READ_FAILED.; |
| 131 | } |
| 132 | |
| 133 | auto dCurrentEntHandle = spModelSpace->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).hEntitiescxx . . $ CADBlockHeaderObject#hEntities.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).0].getAsLongcxx . . $ CADHandle#getAsLong(57a65ae535d7db84).(); |
| 134 | auto dLastEntHandle = spModelSpace->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).hEntitiescxx . . $ CADBlockHeaderObject#hEntities.[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).1].getAsLongcxx . . $ CADHandle#getAsLong(57a65ae535d7db84).(); |
| 135 | // To avoid infinite loops |
| 136 | stdcxx . . $ std/::setcxx . . $ std/set#<long> oVisitedHandlescxx . . $ std/set#set(49f6e7a06ebc5aa8).; |
| 137 | while( dCurrentEntHandle != 0 && |
| 138 | oVisitedHandles.findcxx . . $ std/set#find(f8922f46a681e6cc).(dCurrentEntHandle) ==cxx . . $ std/`operator==`(a84eb84a7a5e8237). oVisitedHandles.endcxx . . $ std/set#end(3220b82c0db6d2a1).() ) |
| 139 | { |
| 140 | oVisitedHandles.insertcxx . . $ std/set#insert(411a10a755068dfe).(dCurrentEntHandle); |
| 141 | |
| 142 | CADObjectcxx . . $ CADObject#* pCADEntityObject = pCADFile->GetObjectcxx . . $ CADFile#GetObject(782acc3ee44d2069).( dCurrentEntHandle, true ); |
| 143 | unique_ptrcxx . . $ std/unique_ptr#<CADEntityObjectcxx . . $ CADEntityObject#> spEntityObjcxx . . $ std/unique_ptr#unique_ptr(c8c3025d73122d7d).( |
| 144 | dynamic_cast<CADEntityObjectcxx . . $ CADEntityObject# *>( pCADEntityObject ) ); |
| 145 | |
| 146 | if( !spEntityObj ) |
| 147 | { |
| 148 | delete pCADEntityObject; |
| 149 | DebugMsgcxx . . $ DebugMsg(326aeb464d16410c).( "Entity object is null\n" ); |
| 150 | break; |
| 151 | } |
| 152 | else if ( dCurrentEntHandle == dLastEntHandle ) |
| 153 | { |
| 154 | FillLayercxx . . $ CADTables#FillLayer(e511d483495c12e5).( spEntityObj.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).() ); |
| 155 | break; |
| 156 | } |
| 157 | |
| 158 | FillLayercxx . . $ CADTables#FillLayer(e511d483495c12e5).( spEntityObj.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).() ); |
| 159 | |
| 160 | if( spEntityObj->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).stCedcxx . . $ CADEntityObject#stCed..bNoLinkscxx . . $ CADCommonED#bNoLinks. ) |
| 161 | { |
| 162 | ++dCurrentEntHandle; |
| 163 | } |
| 164 | else |
| 165 | { |
| 166 | dCurrentEntHandle = spEntityObj->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).stChedcxx . . $ CADEntityObject#stChed..hNextEntitycxx . . $ CADCommonEHD#hNextEntity..getAsLongcxx . . $ CADHandle#getAsLong(f8e9dc47f2e00c01).( spEntityObj->cxx . . $ std/unique_ptr#`operator->`(ea7391e9eeeaf20e).stCedcxx . . $ CADEntityObject#stCed..hObjectHandlecxx . . $ CADCommonED#hObjectHandle. ); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | DebugMsgcxx . . $ DebugMsg(326aeb464d16410c).( "Read aLayers using LayerControl object count: %d\n", |
| 171 | static_cast<int>(aLayerscxx . . $ CADTables#aLayers..sizecxx . . $ std/vector#size(c9b1e749b64d5edc).()) ); |
| 172 | |
| 173 | return CADErrorCodescxx . . $ CADErrorCodes#::SUCCESScxx . . $ SUCCESS.; |
| 174 | } |
| 175 | |
| 176 | void CADTablescxx . . $ CADTables#::FillLayercxx . . $ CADTables#FillLayer(e511d483495c12e5).( const CADEntityObjectcxx . . $ CADEntityObject# * pEntityObject ) |
| 177 | { |
| 178 | if(nullptr == pEntityObject) |
| 179 | { |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | for( CADLayercxx . . $ CADLayer#& oLayer : aLayerscxx . . $ CADTables#aLayers. ) |
| 184 | { |
| 185 | if( pEntityObject->stChedcxx . . $ CADEntityObject#stChed..hLayercxx . . $ CADCommonEHD#hLayer..getAsLongcxx . . $ CADHandle#getAsLong(f8e9dc47f2e00c01).( |
| 186 | pEntityObject->stCedcxx . . $ CADEntityObject#stCed..hObjectHandlecxx . . $ CADCommonED#hObjectHandle. ) == oLayer.getHandlecxx . . $ CADLayer#getHandle(57a65ae535d7db84).() ) |
| 187 | { |
| 188 | DebugMsgcxx . . $ DebugMsg(326aeb464d16410c).( "Object with type: %s is attached to layer named: %s\n", |
| 189 | getNameByTypecxx . . $ getNameByType(74f53bef2481b9d4).( pEntityObject->getTypecxx . . $ CADObject#getType(f4d3a0c5c09c17f0).() ).c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), |
| 190 | oLayer.getNamecxx . . $ CADLayer#getName(219584694fe2b248).().c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).() ); |
| 191 | |
| 192 | oLayer.addHandlecxx . . $ CADLayer#addHandle(284d981dba628063).( pEntityObject->stCedcxx . . $ CADEntityObject#stCed..hObjectHandlecxx . . $ CADCommonED#hObjectHandle..getAsLongcxx . . $ CADHandle#getAsLong(57a65ae535d7db84).(), |
| 193 | pEntityObject->getTypecxx . . $ CADObject#getType(f4d3a0c5c09c17f0).() ); |
| 194 | break; |
| 195 | } |
| 196 | } |
| 197 | } |