- 积分
- 2191
- 明经币
- 个
- 注册时间
- 2022-4-4
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
 - #pragma once
- #include <acadstrc.h>
- #include <AcApDocWindow.h>
- #include <acarray.h>
- #include <acdb.h>
- #include <acdocman.h>
- #include <acestext.h>
- #include <AcString.h>
- #include <acutads.h>
- #include <adesk.h>
- #include <dbdict.h>
- #include <dbid.h>
- #include <dbmain.h>
- #include <dbObject.h>
- #include <dbsymtb.h>
- #include <dbtrans.h>
- #include <vector>
- #include <Windows.h>
- class DbTrans {
- private:
- Acad::ErrorStatus m_es = Acad::eOk;
- public:
- AcApDocument* pDoc;
- AcDbDatabase* pDb;
- AcDbTransactionManager* pTm;
- AcTransaction* pTrans;
- bool IsOk() const { return m_es == Acad::eOk; }
- DbTrans()
- {
- pDoc = curDoc();
- acDocManager->lockDocument(pDoc);
- pDb = pDoc->database();
- pTm = pDb->transactionManager();
- pTrans = pTm->startTransaction();
- }
- ~DbTrans()
- {
- Commit();
- acDocManager->unlockDocument(pDoc);
- }
- void Commit()
- {
- if (IsOk())
- {
- pTm->endTransaction();
- /*acutPrintf(L"\n提交事务\n");*/
- }
- else
- {
- pTm->abortTransaction();
- acutPrintf(L"\n异常操作,退出事务\n");
- }
- }
- template <class T = AcDbEntity>
- T* getObject(AcDbObjectId objid, AcDb::OpenMode mode = AcDb::OpenMode::kForRead, bool openErased = false)
- {
- T* pObj = nullptr;
- m_es = pTrans->getObject<T>(pObj, objid, mode, openErased);
- if (IsOk()) {
- return pObj;
- }
- else {
- return nullptr;
- }
- }
- template<typename T = AcDbEntity>
- std::vector <T*> getObjects(AcArray<AcDbObjectId> ids, AcDb::OpenMode mode = AcDb::OpenMode::kForRead, bool openErased = false) {
- std::vector<T*> ents;
- for (auto objid : ids) {
- T* pObj = NULL;
- m_es = pTrans->getObject<T>(pObj, objid, mode, openErased);
- if (IsOk()) {
- ents.push_back(pObj);
- }
- }
- return ents;
- }
- template<typename TableType>
- AcDbObjectId getDictId(TableType* table, AcString name) {
- AcDbObjectId tableId = AcDbObjectId::kNull;
- auto es = table->getAt(name, tableId);
- if (es != Acad::eOk)
- acutPrintf(L"不存在%ls的记录\n", name);
- return tableId;
- }
- AcDbBlockTableRecord* currentSpace()
- {
- return this->getObject<AcDbBlockTableRecord>(pDb->currentSpaceId(), AcDb::kForWrite);
- }
- AcDbBlockTableRecord* paperSpace()
- {
- return this->getObject<AcDbBlockTableRecord>(pDb->paperSpaceVportId(), AcDb::kForWrite);
- }
- AcDbBlockTable* getBlockTable() {
- return this->getObject<AcDbBlockTable>(pDb->blockTableId(), AcDb::kForWrite);
- }
- AcDbRegAppTable* getRegAppTable() {
- return this->getObject<AcDbRegAppTable>(pDb->regAppTableId(), AcDb::kForWrite);
- }
- AcDbDimStyleTable* getDimStyleTable() {
- return this->getObject<AcDbDimStyleTable>(pDb->dimStyleTableId(), AcDb::kForWrite);
- }
- AcDbLayerTable* getLayerTable() {
- return this->getObject<AcDbLayerTable>(pDb->layerTableId(), AcDb::kForWrite);
- }
- AcDbLinetypeTable* getLineTypeTable() {
- return this->getObject<AcDbLinetypeTable>(pDb->linetypeTableId(), AcDb::kForWrite);
- }
- AcDbTextStyleTable* getTextStyleTable() {
- return this->getObject<AcDbTextStyleTable>(pDb->textStyleTableId(), AcDb::kForWrite);
- }
- AcDbUCSTable* getUcsTable() {
- return this->getObject<AcDbUCSTable>(pDb->UCSTableId(), AcDb::kForWrite);
- }
- AcDbViewTable* getViewTable() {
- return this->getObject<AcDbViewTable>(pDb->viewTableId(), AcDb::kForWrite);
- }
- AcDbViewportTable* getViewportTable() {
- return this->getObject<AcDbViewportTable>(pDb->viewportTableId(), AcDb::kForWrite);
- }
- AcDbDictionary* getMLeaderStyleDictionary() {
- return this->getObject<AcDbDictionary>(pDb->mleaderStyleDictionaryId(), AcDb::kForWrite);
- }
- AcDbDictionary* getMlineStyleDictionary() {
- return this->getObject<AcDbDictionary>(pDb->mLStyleDictionaryId(), AcDb::kForWrite);
- }
- AcDbDictionary* getMaterialDictionary() {
- return this->getObject<AcDbDictionary>(pDb->materialDictionaryId(), AcDb::kForWrite);
- }
- AcDbDictionary* getPlotSettingsDictionary() {
- return this->getObject<AcDbDictionary>(pDb->plotSettingsDictionaryId(), AcDb::kForWrite);
- }
- AcDbDictionary* getPlotStyleNameDictionary() {
- return this->getObject<AcDbDictionary>(pDb->plotStyleNameDictionaryId(), AcDb::kForWrite);
- }
- AcDbDictionary* getLayoutDictionary() {
- return this->getObject<AcDbDictionary>(pDb->layoutDictionaryId(), AcDb::kForWrite);
- }
- AcDbObjectId addEntity(AcDbBlockTableRecord* btr, AcDbEntity* ent)
- {
- AcDbObjectId objId;
- if (!btr) {
- acutPrintf(L"\n错误:块表记录指针为空");
- return objId;
- }
- Acad::ErrorStatus es = btr->appendAcDbEntity(objId, ent);
- if (es != Acad::eOk) {
- acutPrintf(L"\n错误:无法将实体添加到块表记录 (错误码: %s)", acadErrorStatusText(es));
- return objId;
- }
- es = pTm->addNewlyCreatedDBRObject(ent, true);
- if (es != Acad::eOk) {
- acutPrintf(L"\n错误:无法将实体添加到事务管理器 (错误码: %s)", acadErrorStatusText(es));
- return objId;
- }
- return objId;
- }
- template<typename T>
- AcArray<AcDbObjectId> addEntities(AcDbBlockTableRecord* btr, AcArray<T*> ents)
- {
- AcArray<AcDbObjectId> objIds;
- for (AcDbEntity* ent : ents) {
- auto objId = addEntity(btr, ent);
- if (objId == AcDbObjectId::kNull) continue;
- objIds.append(objId);
- }
- return objIds;
- }
- bool upopen(AcDbObject* pobj) {
- // 确保对象在事务中
- bool isWritable = pobj->isWriteEnabled();
- if (!isWritable) {
- pobj->upgradeOpen();
- return pobj->isWriteEnabled();
- }
- return isWritable;
- }
- void erase(AcDbObject* pObj) {
- if (!pObj->isErased() && !pObj->isNewObject()) {
- if (upopen(pObj)) {
- pObj->erase();
- }
- }
- }
- void flushScreen()
- {
- pTm->queueForGraphicsFlush();
- }
- };
- inline static void setFocusDoc() {
- auto windowHwnd = acDocWindowManager->activeDocumentWindow()->windowHandle();
- SetFocus(windowHwnd);
- }
使用示例:不需要对line 进行close 或者delete
- void addLine() {
- DbTrans tr;
- auto btr = tr.currentSpace();
- AcDbLine* line = new AcDbLine({ 0,0,0 }, { 50,50,0 });
- tr.addEntity(btr, line);
- }
复制代码
|
|