#include "stdafx.h" #include "LayerMgr.h" #include "KTCADUtilityEx.h" #include "uiiado.h" #include "coreTools.h" #include "KTSetting.h" #define LAYER_FILE _T("LayerMgr.db"); #define GET_VALUE(value,idx) ly.value = TypeConvert::convert((LPCTSTR)arr[idx]) namespace { AcDbObjectId GetLinetypeIdFromString(const TCHAR* pStr, AcDbDatabase *pDb = NULL) { if (!pDb) pDb = acdbCurDwg(); AcDbObjectId id(AcDbObjectId::kNull); if (!pStr) return id; AcDbLinetypeTable* pLinetypeTable(NULL); Acad::ErrorStatus err = pDb->getLinetypeTable(pLinetypeTable, AcDb::kForRead); if (err == Acad::eOk && pLinetypeTable) { err = pLinetypeTable->getAt(pStr, id); pLinetypeTable->close(); } return id; } bool getLayerData(LPCTSTR pszLayer, CLayerMgr::Layer &ly) { CString path = FilePath::GetSysDir(); path += _T("\\"); path += LAYER_FILE; CUIISQLiteServer dbSQLite; if (!dbSQLite.InitADOServer(path)) return false; CString sql(_T("")); sql.Format(_T("select * from layer where name='%s'"), pszLayer); CStringArray arr; dbSQLite.GetSelData(sql, arr); if (arr.GetCount() < 9) return false; ly.name = arr[0]; ly.colorIdx = TypeConvert::convert((LPCTSTR)arr[1]); GET_VALUE(isHidden, 2); GET_VALUE(isFrozen, 3); GET_VALUE(isLocked, 4); GET_VALUE(isOff, 5); GET_VALUE(isPlottable, 6); GET_VALUE(lw, 7); ly.linetype = arr[8]; return true; } AcDbObjectId getLinetypeID(LPCTSTR psz) { AcDbObjectId linetype(AcDbObjectId::kNull); if (!psz) return linetype; AcDbDatabase *pDb = acdbHostApplicationServices()->workingDatabase(); if (!pDb) return linetype; AcDbLinetypeTable *pTbl(NULL); pDb->getLinetypeTable(pTbl, AcDb::kForRead); if (!pTbl) return linetype; pTbl->getAt(psz, linetype); pTbl->close(); return linetype; } void setLayerData(AcDbLayerTableRecord *pRc, const CLayerMgr::Layer &ly) { if (!pRc) return; AcCmColor clr; clr.setColorIndex(ly.colorIdx); pRc->setColor(clr); pRc->setName(ly.name); pRc->setIsHidden(ly.isHidden == 0 ? false : true); pRc->setIsFrozen(ly.isFrozen == 0 ? false : true); pRc->setIsLocked(ly.isLocked == 0 ? false : true); pRc->setIsOff(ly.isOff == 0 ? false : true); pRc->setIsPlottable(ly.isPlottable == 0 ? false : true); pRc->setLineWeight((AcDb::LineWeight)ly.lw); AcDbObjectId linetype(getLinetypeID(ly.linetype)); if (linetype.isValid()) pRc->setLinetypeObjectId(linetype); } } CLayerMgr::CLayerMgr() { } CLayerMgr::~CLayerMgr() { } bool CLayerMgr::isExistInLayerTbl(LPCTSTR pszLayer) { AcDbDatabase *pDb = acdbHostApplicationServices()->workingDatabase(); if (!pDb) return false; AcDbLayerTable *pTbl(NULL); pDb->getLayerTable(pTbl, AcDb::kForRead); if (!pTbl) return false; bool bHas = pTbl->has(pszLayer); pTbl->close(); return bHas; } Acad::ErrorStatus CLayerMgr::setLayer(AcDbObjectId &id, LPCTSTR pszLayer) { if (!pszLayer) return Acad::eInvalidInput; OPENOBJ_BEGIN(id, AcDb::kForWrite, AcDbEntity, pEnt) if (pEnt) return setLayer(pEnt, pszLayer); OPENOBJ_END() return Acad::eInvalidInput; } Acad::ErrorStatus CLayerMgr::setLayer(AcDbEntity *pEnt, LPCTSTR pszLayer) { if (!pszLayer) return Acad::eInvalidInput; AcDbObjectId layerID(AcDbObjectId::kNull); Acad::ErrorStatus es = initLayer(pszLayer, &layerID); if (es != Acad::eOk) return es; pEnt->setLinetype(GetLinetypeIdFromString(_T("BYLAYER"))); pEnt->setLineWeight(AcDb::kLnWtByLayer); pEnt->setColorIndex(256); es = pEnt->setLayer(layerID); return es; } Acad::ErrorStatus CLayerMgr::getLayer(LPCTSTR pszLayer, AcDbObjectId &id) { Acad::ErrorStatus es = initLayer(pszLayer, &id); return es; } Acad::ErrorStatus CLayerMgr::initLayer(LPCTSTR pszLayer, AcDbObjectId *pId) { if (!pszLayer) return Acad::eInvalidInput; AcDbObjectId id = KTSetting::InitLayer(pszLayer); if (!id.isValid()) return Acad::eInvalidInput; if (pId) *pId = id; return Acad::eOk; CLayerMgr::Layer ly; if (!getLayerData(pszLayer, ly)) return Acad::eInvalidInput; AcDbDatabase *pDb = acdbHostApplicationServices()->workingDatabase(); if (!pDb) return Acad::eWrongDatabase; AcDbLayerTable *pTbl(NULL); pDb->getLayerTable(pTbl, AcDb::kForWrite); if (!pTbl) return Acad::eWrongDatabase; AcDbLayerTableRecord *pRc(NULL); if (pTbl->has(pszLayer)) { pTbl->getAt(pszLayer, pRc, AcDb::kForWrite); setLayerData(pRc, ly); } else { pRc = new AcDbLayerTableRecord; setLayerData(pRc, ly); pTbl->add(pRc); } if (pId) *pId = pRc->objectId(); pRc->close(); pTbl->close(); return Acad::eOk; }