#include "StdAfx.h" #include "utility.h" #include "TGAs.h" static XStringList g_lstWinfont; static DList g_lstCharset, g_lstPitch; int utility::CompareString( const CString& strFirst, const CString& strSecond ) { int nRet = ::CompareString(GetThreadLocale(), NORM_IGNOREWIDTH|SORT_STRINGSORT, strFirst,strFirst.GetLength(), strSecond, strSecond.GetLength()/*, NULL, NULL, 0*/); if(nRet == CSTR_LESS_THAN) //升序 return -1; else if(nRet == CSTR_GREATER_THAN) return 1; else return 0; } CString utility::GetInstallDir() { CString str(g_sAppPath), str1(g_sAppPath); str.MakeUpper(); int iIndex = str.Find(_T("\\SYS")); if (iIndex != -1) { str = str1.Left(iIndex); } else { AfxMessageBox(_T("获取安装目录失败!")); str = _T(""); } return str; } CString utility::GetCurFileName(AcDbDatabase *pData) { if (!pData) return _T(""); AcApDocument *pDoc = acDocManager->document(pData); if (!pDoc) return _T(""); CString strFile; strFile.Format(_T("%s"),pDoc->fileName()); int iIndex = strFile.Find('.'); if (iIndex != -1) { strFile = strFile.Left(iIndex); } return strFile; } void utility::GetAccmColorByString(CString sColor, AcCmColor &color) { int iColor(0); bool bColor(false); CString sSwap(sColor); if (sSwap.Find(_T(',')) == -1) // 索引值 { if(sSwap == _T("红")) { iColor = 1; } else if(sSwap == _T("黄")) { iColor = 2; } else if(sSwap == _T("绿")) { iColor = 3; } else if(sSwap == _T("青")) { iColor = 4; } else if(sSwap == _T("蓝")) { iColor = 5; } else if(sSwap == _T("洋红")) { iColor = 6; } else if(sSwap == _T("白") || sSwap == _T("黑")) { iColor = 7; } else if(sSwap == _T("BYLAYER")) { color.setColorMethod(AcCmEntityColor::kByLayer); return; } else if(sSwap == _T("BYBLOCK")) { color.setColorMethod(AcCmEntityColor::kByBlock); return; } else { iColor = _ttoi(sColor); } } else // RGB颜色值 { bColor = true; } if (bColor) { int iIndex = sSwap.Find(','); CString sR(sSwap.Left(iIndex)); sSwap = sSwap.Right(sSwap.GetLength() - iIndex - 1); iIndex = sSwap.Find(','); CString sG(sSwap.Left(iIndex)); CString sB(sSwap.Right(sSwap.GetLength() - iIndex - 1)); Acad::ErrorStatus bOk = color.setRGB(Adesk::UInt8(_ttoi(sR)), Adesk::UInt8(_ttoi(sG)), Adesk::UInt8(_ttoi(sB))); } else { color.setColorIndex(iColor); } } AcDbObjectId utility::GetIdByName(LPCTSTR name) { AcDbObjectId id = AcDbObjectId::kNull; AcDbLinetypeTable *pTbl = NULL; acdbCurDwg()->getSymbolTable(pTbl, AcDb::kForRead); if (!pTbl) return id; do { if (pTbl->has(name)) pTbl->getAt(name, id); else break; } while (false); pTbl->close(); if (id.isNull()) { id = InitLineType(name); if (id.isNull()) id = InitTchLineType(name); if (id.isNull()) id = LoadTArch20LineType(name); } return id; } bool utility::ReadHatchMaterialConfigFile( const CString& strMaterialType, std::vector< std::pair >& materialName ) { CString strMaerial = _T("["); strMaerial += strMaterialType; strMaerial += _T("]"); CStdioFile file; if(!file.Open(utility::GetHatchMaterialConfigFilePath(), CFile::modeRead)) return false; CString strLine; bool bFindMaterial = false; while(file.ReadString(strLine)) { if(strMaerial.CompareNoCase(strLine) == 0) { bFindMaterial = true; break; } } if(!bFindMaterial) { file.Close(); return false; } CString strLeft, strRight; int nPos = -1; int nLenght; materialName.clear(); while(file.ReadString(strLine)) { if(strLine.IsEmpty()) continue; strLine.Trim(); if(strLine[0] == _T('[')) //下一种材料开始 break; nPos = strLine.Find(_T('*')); if(nPos == -1) { strLeft = strLine; strRight = strLine; } else { nLenght = strLine.GetLength(); strLeft = strLine.Left(nPos); strRight = strLine.Right(nLenght - nPos - 1); } strLeft.Trim(_T(" ,,")); strRight.Trim(_T(" ,,")); materialName.push_back(std::make_pair(strLeft, strRight)); } file.Close(); return true; } CString utility::getConfigFilePath( const CString& fileName ) { CString sPath; ::GetModuleFileName(_hdllInstance, sPath.GetBuffer(MAX_PATH), MAX_PATH); sPath.ReleaseBuffer(); sPath = sPath.Left(sPath.ReverseFind(_T('\\'))); sPath = sPath.Left(sPath.ReverseFind(_T('\\'))); sPath += _T("\\SYS\\"); sPath += fileName; return sPath; } AcDbObjectId utility::LoadTArch20LineType(const TCHAR* szLineType) { AcDbObjectId id = GetTArch20LinetypeIdFromString(szLineType); if(id != AcDbObjectId::kNull) return id; XString xsDbfFile; if(!FindFile(_T("linetype.dbf"), xsDbfFile)) { AfxMessageBox(_T("对不起,没有发现linetype.dbf文件!")); return id; } CodeBase db; XDbfTable *pTable = db.Open(xsDbfFile); if(!pTable) { CString csTmp; csTmp.Format(_T("%s文件只读或损坏!"), xsDbfFile); AfxMessageBox(csTmp); // AfxMessageBox(_T("TK文件只读或损坏!")); return id; } for(pTable->MoveFirst();!pTable->IsEOF();pTable->MoveNext()){ CString sName, sComment, sContent, sNum, sFit, sLength; GET_FIELD(pTable, _T("Name"), sName); if(_tcsicmp(sName, szLineType) == 0){ GET_FIELD(pTable, _T("Comment"), sComment); GET_FIELD(pTable, _T("Num"), sNum); GET_FIELD(pTable, _T("Scale"), sFit); GET_FIELD(pTable, _T("Length"), sLength); GET_FIELD(pTable, _T("Content"), sContent); bool bFit; if(sFit == _T("1")) bFit = true; else bFit = false; LoadLineType((const TCHAR*)sName, (const TCHAR*)sComment, _ttoi(sNum), bFit, _tstof(sLength), (const TCHAR*)sContent); id = GetTArch20LinetypeIdFromString((const TCHAR*)sName); break; } } return id; } int CALLBACK EnumFontFamExProc2(ENUMLOGFONTEX *lplf, NEWTEXTMETRICEX *lpntme, int FontType, LONG lParam) { if (!(FontType & TRUETYPE_FONTTYPE)) return 1 ; if (*(lplf->elfLogFont.lfFaceName)) { if(!_tcsicmp(lplf->elfLogFont.lfFaceName, _T("marlett"))) return 1; if(g_lstWinfont.Index(lplf->elfLogFont.lfFaceName, 0) < 0 && _tcsicmp((const TCHAR*)lplf->elfScript, _T("CHINESE_GB2312")) == 0) {// winFont g_lstWinfont.Append(tstrnew(lplf->elfLogFont.lfFaceName)); g_lstCharset.Append(new int(lplf->elfLogFont.lfCharSet)); g_lstPitch.Append(new int(lplf->elfLogFont.lfPitchAndFamily)); } } return 1 ; } int utility::GetPScale() { double nScale=DocGetPScale(); int cvport; VERIFY(xg_getvar(_T("CVPORT"), &cvport)==RTNORM); if(cvport==1) nScale=1.0; // return ads_retint( AS_INT(nScale)); return (int)nScale; } AcDbObjectId utility::InitLineTypeTextStyle_Thunder() { CString g_csLineTypeChnShx = _T("HZTXT"); CString g_csLineTypeEngShx = _T("SIMPLEX"); double g_dLineTypeScale = 0.8; AcDbObjectId idRet=GetTextStyleId(_T("TG_LINETYPE_THUND")); if(idRet == NULL) { g_lstWinfont.Reset(); g_lstCharset.Reset(); g_lstPitch.Reset(); CDC *pDC = acedGetAcadDwgView()->GetDC(); ASSERT(pDC); LOGFONT lf; ZeroMemory(&lf,sizeof(lf)); lf.lfCharSet = DEFAULT_CHARSET; EnumFontFamiliesEx(pDC->m_hDC, &lf, (FONTENUMPROC)EnumFontFamExProc2, NULL, 0); acedGetAcadDwgView()->ReleaseDC(pDC); AcDbTextStyleTableRecord *pStyle=new AcDbTextStyleTableRecord; pStyle->setName(_T("TG_LINETYPE_THUND")); pStyle->setTextSize(5.5); int idx = g_lstWinfont.Index(g_csLineTypeChnShx); if(idx < 0){ pStyle->setFileName(g_csLineTypeEngShx); pStyle->setBigFontFileName(g_csLineTypeChnShx); } else{ int iCharset = *g_lstCharset[idx]; int iPitch = *g_lstPitch[idx]; #if ADS < 21 pStyle->setFont(g_csLineTypeChnShx, FALSE, FALSE, iCharset, iPitch); #else typedef Autodesk::AutoCAD::PAL::FontUtils::FontPitch FontPitch; typedef Autodesk::AutoCAD::PAL::FontUtils::FontFamily FontFamily; FontPitch pitch((FontPitch)(iPitch & 0x3)); FontFamily family((FontFamily)(iPitch & 0xF0)); pStyle->setFont(g_csLineTypeChnShx, FALSE, FALSE, (Charset)iCharset, pitch, family); #endif } pStyle->setXScale(g_dLineTypeScale); idRet = AddTextStyle(pStyle, TRUE); } return idRet; } AcDbObjectId utility::InitLineTypeTextStyle() { CString g_csLineTypeStyle = _T("TG_LINETYPE"); CString g_csLineTypeChnShx = _T("HZTXT"); CString g_csLineTypeEngShx = _T("SIMPLEX"); double g_dLineTypeHeight = 3; double g_dLineTypeScale = 0.8; if (g_nProductId == 16) { //修改电气的TG_LINETYPE线型设置注册表读取位置 axc add 2015-09-01 CRegProperty regProp(GetRegLocation(), _T("TElecConfig")); regProp.SetTopRootKey(HKEY_CURRENT_USER); regProp.GetValue(_T("LineTypeChnShx"), g_csLineTypeChnShx); regProp.GetValue(_T("LineTypeEngShx"), g_csLineTypeEngShx); regProp.GetValue(_T("LineTypeHeight"), g_dLineTypeHeight); regProp.GetValue(_T("LineTypeScale"), g_dLineTypeScale); } else { CRegProperty regProp(GetRegLocation(), _T("TWTConfig")); regProp.SetTopRootKey(HKEY_CURRENT_USER); regProp.GetValue(_T("LineTypeChnShx"), g_csLineTypeChnShx); regProp.GetValue(_T("LineTypeEngShx"), g_csLineTypeEngShx); regProp.GetValue(_T("LineTypeHeight"), g_dLineTypeHeight); regProp.GetValue(_T("LineTypeScale"), g_dLineTypeScale); } AcDbObjectId idRet=GetTextStyleId(g_csLineTypeStyle); if(idRet == NULL) { g_lstWinfont.Reset(); g_lstCharset.Reset(); g_lstPitch.Reset(); CDC *pDC = acedGetAcadDwgView()->GetDC(); ASSERT(pDC); LOGFONT lf; ZeroMemory(&lf,sizeof(lf)); lf.lfCharSet = DEFAULT_CHARSET; EnumFontFamiliesEx(pDC->m_hDC, &lf, (FONTENUMPROC)EnumFontFamExProc2, NULL, 0); acedGetAcadDwgView()->ReleaseDC(pDC); AcDbTextStyleTableRecord *pStyle=new AcDbTextStyleTableRecord; pStyle->setName(g_csLineTypeStyle); pStyle->setTextSize(g_dLineTypeHeight); int idx = g_lstWinfont.Index(g_csLineTypeChnShx); if(idx < 0){ pStyle->setFileName(g_csLineTypeEngShx); pStyle->setBigFontFileName(g_csLineTypeChnShx); } else{ int iCharset = *g_lstCharset[idx]; int iPitch = *g_lstPitch[idx]; #if ADS < 21 pStyle->setFont(g_csLineTypeChnShx, FALSE, FALSE, iCharset, iPitch); #else typedef Autodesk::AutoCAD::PAL::FontUtils::FontPitch FontPitch; typedef Autodesk::AutoCAD::PAL::FontUtils::FontFamily FontFamily; FontPitch pitch((FontPitch)(iPitch & 0x3)); FontFamily family((FontFamily)(iPitch & 0xF0)); pStyle->setFont(g_csLineTypeChnShx, FALSE, FALSE, (Charset)iCharset, pitch, family); #endif } pStyle->setXScale(g_dLineTypeScale); idRet = AddTextStyle(pStyle, TRUE); } return idRet; } void utility::GetLineDashData(int i, CString csContent, double &dLen, CString &csText, CString &csStyle, double &dScale, double &dRotation, double &dX, double &dY) { int idx1 = csContent.Find(_T("[")); int idx(0); CString csDesh; while(idx1>-1){ int idx2 = csContent.Find(_T("]"), idx1); if(idx2 < 0) break; CString csTmp; csTmp = csContent.Mid(idx1+1, idx2-idx1-1); if(idx == i){ csDesh = csTmp; break; } idx++; idx1 = csContent.Find(_T("["), idx2); } if(csDesh == _T("")) return; idx1 = csDesh.Find(_T(",")); CString csTmp = csDesh.Mid(0, idx1); dLen = _tstof(csTmp); int idx2 = csDesh.Find(_T(","), idx1+1); idx=1; while(idx1 > -1){ csTmp = csDesh.Mid(idx1+1, idx2-idx1-1); idx1 = idx2; idx2 = csDesh.Find(_T(","), idx1+1); if(idx == 1) csText = csTmp; else if(idx == 2) csStyle = csTmp; else if(idx == 3) dScale = _tstof(csTmp); else if(idx == 4) dRotation = _tstof(csTmp); else if(idx == 5) dX = _tstof(csTmp); else if(idx == 6) dY = _tstof(csTmp); idx++; if(idx2<0) break; } } AcDbObjectId utility::CreateShxStyle(CString csShxFile) { AcDbObjectId idRet=GetTextStyleId(csShxFile); if(idRet == NULL) { AcDbTextStyleTableRecord *pStyle=new AcDbTextStyleTableRecord; pStyle->setName(csShxFile); pStyle->setFileName(csShxFile); pStyle->setIsShapeFile(TRUE); idRet = AddTextStyle(pStyle, TRUE); } return idRet; } BOOL utility::LoadLineType(CString csName, CString csComment, int num, bool fit, double dLength, CString csContent) { // AcDbObjectId idRet=InitLineTypeTextStyle(); if(csName.Find(_T("×")) > -1) InitLineTypeTextStyle_Thunder(); else InitLineTypeTextStyle(); AcDbLinetypeTable *pLinetypeTbl = NULL; acdbCurDwg()->getLinetypeTable(pLinetypeTbl, AcDb::kForWrite); if(pLinetypeTbl == NULL){ return FALSE; } BOOL bHasIt = pLinetypeTbl->has((const TCHAR*)csName); if(bHasIt){ pLinetypeTbl->close(); adsout << _T("\n已经存在同名线型!"); return FALSE; } AcDbLinetypeTableRecord* newRec = new AcDbLinetypeTableRecord; newRec->setName(csName); const TCHAR* szName; newRec->getName(szName); if(_tcsicmp(szName, _T("")) == 0){ pLinetypeTbl->close(); adsout << _T("\n线形名非法,建议使用全角!"); delete newRec; return FALSE; } newRec->setAsciiDescription(csComment); newRec->setComments(csComment); newRec->setPatternLength(dLength); newRec->setNumDashes(num); newRec->setIsScaledToFit(fit); for(int i=0;isetDashLengthAt(i, dLen); if(csText != _T("")){// && fabs(dLen) > 0.0001 sr remove it 2007-3-22 TG_x 不支持 if(csStyle.Find(_T(".shx")) > 0) { //2008-11-18 支持形文件 AcDbObjectId idTextStyle = CreateShxStyle(csStyle); newRec->setShapeStyleAt(i, idTextStyle); int iShapeNum = _ttoi(csText); newRec->setShapeNumberAt(i, iShapeNum); newRec->setShapeScaleAt(i, dScale); newRec->setShapeRotationAt(i, dRotaion); AcGeVector2d vt(-dX, -dY); newRec->setShapeOffsetAt(i, vt); } else { AcDbObjectId idTextStyle = GetTextStyleId(csStyle); newRec->setShapeStyleAt(i, idTextStyle); newRec->setTextAt(i, csText); newRec->setShapeScaleAt(i, dScale); newRec->setShapeRotationAt(i, dRotaion); AcGeVector2d vt(-dX, -dY); newRec->setShapeOffsetAt(i, vt); } } } pLinetypeTbl->add(newRec); newRec->close(); pLinetypeTbl->close(); return TRUE; } AcDbObjectId utility::GetTArch20LinetypeIdFromString(const TCHAR* pStr) { AcDbDatabase *pDb = acdbCurDwg(); AcDbObjectId id = AcDbObjectId::kNull; if(pStr) { AcDbLinetypeTable* pLinetypeTable = NULL; //modified by whl on 060301 //Acad::ErrorStatus err = acdbCurDwg()->getLinetypeTable( Acad::ErrorStatus err = pDb->getLinetypeTable(pLinetypeTable, AcDb::kForRead); if (err == Acad::eOk && pLinetypeTable) { err = pLinetypeTable->getAt(pStr, id); pLinetypeTable->close(); } } return id; } CString utility::GetHatchMaterialConfigFilePath() { return getConfigFilePath(_T("TArch20HatchMaterialConfig.txt")); }