commit 163e14ad2559ddd6d3385732ff8bb464a40305e3 Author: natexvi Date: Fri Feb 20 14:27:51 2026 -0800 initial commit diff --git a/dyn-table.dwg b/dyn-table.dwg new file mode 100644 index 0000000..4551023 Binary files /dev/null and b/dyn-table.dwg differ diff --git a/example1.cpp b/example1.cpp new file mode 100644 index 0000000..c6aacdd --- /dev/null +++ b/example1.cpp @@ -0,0 +1,117 @@ +/* + * Example 2 + * + * This program copies the first block reference from the + * example drawing to a new drawing file. + */ +#include "OdaCommon.h" + +#include "DbBlockTableRecord.h" +#include "DbDatabase.h" +#include "DbIdMapping.h" +#include "DbObject.h" +#include "DbObjectId.h" +#include "DbSystemServices.h" +#include "DynamicLinker.h" +#include "ExHostAppServices.h" +#include "ExPrintConsole.h" +#include "ExSystemServices.h" +#include "IdArrays.h" +#include "OdCodePage.h" +#include "OdModuleNames.h" +#include "RxInit.h" +#include "diagnostics.h" +#include + +static void +MyAssert(const char *expression, const char *fileName, int nLineNo) { + OdString message; + message.format(L"\n!!! Assertion failed: \"%s\"\n file: %ls, line %d\n\n", + OdString(expression).c_str(), + OdString(fileName).c_str(), + nLineNo); + odPrintConsoleString(message); +} + +class MyServices : public ExSystemServices, public ExHostAppServices {}; + +void InitServices(OdStaticRxObject *svcs) { + odSetAssertFunc(MyAssert); + odInitialize(svcs); + svcs->disableOutput(true); + + // load dynamic block related modules + ::odrxInitialize(svcs); + ::odrxDynamicLinker()->loadModule(DbConstraintsModuleName); + ::odrxDynamicLinker()->loadModule(OdDynBlocksModuleName); + + // set codepage to avoid AutoCAD warning pop-up when creating DWGs from linux + svcs->setSystemCodePage(OdCodePageId::CP_ANSI_1252); +} + +void UninitializeServices(OdStaticRxObject *svcs) { + odrxUninitialize(); + odUninitialize(); +} + +void Run(OdStaticRxObject *svcs); + +int main(int argc, char **argv) { + // create and initialize the services + OdStaticRxObject svcs; + InitServices(&svcs); + + // try to run the code and catch any exceptions + try { + Run(&svcs); + } catch(const OdError &e) { + odPrintConsoleString(L"OdError: "); + odPrintConsoleString(e.description()); + odPrintConsoleString(L"\n"); + } catch(const std::exception &e) { + std::wcout << L"std::exception: " << e.what() << std::endl; + } catch(...) { odPrintConsoleString(L"Unknown exception\n"); } + + // uninitialize the services and exit + UninitializeServices(&svcs); + return 0; +} + +void Run(OdStaticRxObject *svcs) { + // read the example drawing + OdDbDatabasePtr sourceDb = svcs->readFile(L"./dyn-table.dwg"); + // create a new empty drawing that the contents of the + // example drawing will be copied to + OdDbDatabasePtr targetDb = svcs->createDatabase(); + + // get the id of the first block reference in the example file + OdDbObjectIdArray sourceIds; + OdDbBlockTableRecordPtr sourceMsp = sourceDb->getModelSpaceId().openObject(); + // this is the handle of the first block reference in the example drawing + OdString targetHandle = "25D"; + bool found = false; + for(auto iter = sourceMsp->newIterator(); !iter->done(); iter->step()) { + OdDbObjectId id = iter->objectId(); + OdString handle = id.getHandle().ascii(); + if(handle == targetHandle) { + sourceIds.push_back(id); + found = true; + } + } + + if(!found) { + odPrintConsoleString(L"Error: entity with handle " + targetHandle + + L" not found in the source drawing.\n"); + return; + } + + // copy the entities to the new drawing + OdDbObjectId targetMspId = targetDb->getModelSpaceId(); + OdDbIdMappingPtr idMap = OdDbIdMapping::createObject(); + targetDb->wblockCloneObjects( + sourceIds, targetMspId, *idMap, OdDb::kDrcIgnore); + + // write the new drawing to disk + targetDb->writeFile(L"./example2.dwg", OdDb::kDwg, OdDb::kDHL_CURRENT); + odPrintConsoleString(L"Success: created example2.dwg\n"); +} diff --git a/example1.mp4 b/example1.mp4 new file mode 100644 index 0000000..eccfc52 Binary files /dev/null and b/example1.mp4 differ diff --git a/example2.cpp b/example2.cpp new file mode 100644 index 0000000..478dfb9 --- /dev/null +++ b/example2.cpp @@ -0,0 +1,103 @@ +/* + * Example 1 + * + * This program copies all the model space entities from the + * example drawing to a new drawing file. + */ +#include "OdaCommon.h" + +#include "DbBlockTableRecord.h" +#include "DbDatabase.h" +#include "DbIdMapping.h" +#include "DbObject.h" +#include "DbObjectId.h" +#include "DbSystemServices.h" +#include "DynamicLinker.h" +#include "ExHostAppServices.h" +#include "ExPrintConsole.h" +#include "ExSystemServices.h" +#include "IdArrays.h" +#include "OdCodePage.h" +#include "OdModuleNames.h" +#include "RxInit.h" +#include "diagnostics.h" +#include + +static void +MyAssert(const char *expression, const char *fileName, int nLineNo) { + OdString message; + message.format(L"\n!!! Assertion failed: \"%s\"\n file: %ls, line %d\n\n", + OdString(expression).c_str(), + OdString(fileName).c_str(), + nLineNo); + odPrintConsoleString(message); +} + +class MyServices : public ExSystemServices, public ExHostAppServices {}; + +void InitServices(OdStaticRxObject *svcs) { + odSetAssertFunc(MyAssert); + odInitialize(svcs); + svcs->disableOutput(true); + + // load dynamic block related modules + ::odrxInitialize(svcs); + ::odrxDynamicLinker()->loadModule(DbConstraintsModuleName); + ::odrxDynamicLinker()->loadModule(OdDynBlocksModuleName); + + // set codepage to avoid AutoCAD warning pop-up when creating DWGs from linux + svcs->setSystemCodePage(OdCodePageId::CP_ANSI_1252); +} + +void UninitializeServices(OdStaticRxObject *svcs) { + odrxUninitialize(); + odUninitialize(); +} + +void Run(OdStaticRxObject *svcs); + +int main(int argc, char **argv) { + // create and initialize the services + OdStaticRxObject svcs; + InitServices(&svcs); + + // try to run the code and catch any exceptions + try { + Run(&svcs); + } catch(const OdError &e) { + odPrintConsoleString(L"OdError: "); + odPrintConsoleString(e.description()); + odPrintConsoleString(L"\n"); + } catch(const std::exception &e) { + std::wcout << L"std::exception: " << e.what() << std::endl; + } catch(...) { odPrintConsoleString(L"Unknown exception\n"); } + + // uninitialize the services and exit + UninitializeServices(&svcs); + return 0; +} + +void Run(OdStaticRxObject *svcs) { + // read the example drawing + OdDbDatabasePtr sourceDb = svcs->readFile(L"./dyn-table.dwg"); + // create a new empty drawing that the contents of the + // example drawing will be copied to + OdDbDatabasePtr targetDb = svcs->createDatabase(); + + // get the ids of all the entities in model space + OdDbObjectIdArray sourceIds; + OdDbBlockTableRecordPtr sourceMsp = sourceDb->getModelSpaceId().openObject(); + for(auto iter = sourceMsp->newIterator(); !iter->done(); iter->step()) { + sourceIds.push_back(iter->objectId()); + } + + // copy the entities to the new drawing + OdDbObjectId targetMspId = targetDb->getModelSpaceId(); + OdDbIdMappingPtr idMap = OdDbIdMapping::createObject(); + targetDb->wblockCloneObjects( + sourceIds, targetMspId, *idMap, OdDb::kDrcIgnore); + + // write the new drawing to disk + targetDb->writeFile(L"./example1.dwg", OdDb::kDwg, OdDb::kDHL_CURRENT); + odPrintConsoleString(L"Success: created example1.dwg\n"); +} diff --git a/example2.mp4 b/example2.mp4 new file mode 100644 index 0000000..b25bbbe Binary files /dev/null and b/example2.mp4 differ