- 积分
- 673
- 明经币
- 个
- 注册时间
- 2005-10-29
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
我是新手根据张帆的教程编写了个2004的程序,是将TXT文件导入到CAD的,不知道为什么不能将实体多线段和文字放到相应的图层 以下是代码(写的代码可能有点乱,顺便我想将函数放到类里,不知道怎么的老出错) static void hhabc_abc(void) { //选择文件 const char* title = "选择图形文件"; const char* path = "d:\\"; struct resbuf *FileName; FileName = acutNewRb(RTSTR); if (acedGetFileD(title, path, "txt;dat", 0, FileName) == RTNORM) { acutPrintf(FileName->resval.rstring); }
// 打开所要导入的文本文件 CStdioFile f; CFileException e; if (!f.Open(FileName->resval.rstring, CFile::modeRead, &e)) { acutPrintf("\n打开导入文件失败!"); return; } acutRelRb(FileName); // 获得层表指针 AcDbLayerTable *pLayerTbl; AcDbLayerTableRecord *pLayerTblRcd; acdbHostApplicationServices()->workingDatabase() ->getLayerTable(pLayerTbl, AcDb::kForWrite); // 读取文件中的每一行数据 CString strLineText; // 一行文字 int hh = 1; while (f.ReadString(strLineText)) { // 跳过空行 if (strLineText.IsEmpty()) continue; // 解析出图层名称、颜色、线型和线宽 CStringArray layerInfos; if (!GetFieldText(strLineText, layerInfos)) continue; // 创建新的层表记录,或者打开存在的块表记录 AcDbLayerTableRecord *pLayerTblRcd; AcDbObjectId layerTblRcdId; if (pLayerTbl->has(layerInfos.GetAt(0))) { pLayerTbl->getAt(layerInfos.GetAt(0), layerTblRcdId); } else { pLayerTblRcd = new AcDbLayerTableRecord(); pLayerTblRcd->setName(layerInfos.GetAt(0)); pLayerTbl->add(layerTblRcdId, pLayerTblRcd); } acdbOpenObject(pLayerTblRcd, layerTblRcdId, AcDb::kForWrite);
double x1,x2,x3,x4,y1,y2,y3,y4,xmax,xmin,ymax,ymin,tx,ty; x1 = atof(layerInfos.GetAt(1)); y1 = atof(layerInfos.GetAt(2)); x2 = atof(layerInfos.GetAt(3)); y2 = atof(layerInfos.GetAt(4)); x3 = atof(layerInfos.GetAt(5)); y3 = atof(layerInfos.GetAt(6)); x4 = atof(layerInfos.GetAt(7)); y4 = atof(layerInfos.GetAt(8)); xmax = max(x1,x2,x3,x4); xmin = min(x1,x2,x3,x4); ymax = max(y1,y2,y3,y4); ymin = min(y1,y2,y3,y4); tx = (xmax + xmin)/2; ty = (ymax + ymin)/2; AcGePoint2d pt1(xmin, ymin), pt2(xmin, ymax), pt3(xmax, ymax),pt4(xmax, ymin),pt5(xmin, ymin); AcGePoint2dArray points; points.append(pt1); points.append(pt2); points.append(pt3); points.append(pt4); points.append(pt5); AcDbObjectId aaPoly; aaPoly = CreatePolyline(points,0); ChangeLayer(aaPoly, layerInfos.GetAt(0)); ChangeColor(aaPoly,hh); //添加文字 AcGePoint3d pt6(tx,ty,0); AcDbObjectId aaText; aaText = CreateText(pt6,layerInfos.GetAt(0),AcDbObjectId::kNull,1000,0); ChangeLayer(aaText, layerInfos.GetAt(0)); ChangeColor(aaText,hh); hh= hh++ ; //关闭 pLayerTblRcd->close(); } pLayerTbl->close(); }
//GetFielText函数定义 static BOOL GetFieldText(CString strLineText, CStringArray &fields) { if (strLineText.Find(",", 0) == -1) // 如果找不到英文逗号,函数退出 { return FALSE; } int nLeftPos = 0, nRightPos = 0; // 查找分隔符的起始位置 while ((nRightPos = strLineText.Find(",", nRightPos)) != -1) { fields.Add(strLineText.Mid(nLeftPos, nRightPos - nLeftPos)); nLeftPos = nRightPos + 1; nRightPos++; } // 最后一个列的数据 fields.Add(strLineText.Mid(nLeftPos)); return TRUE; } // 将实体添加到图形数据库的模型空间 static AcDbObjectId PostToModelSpace(AcDbEntity* pEnt) { AcDbBlockTable *pBlockTable; acdbHostApplicationServices()->workingDatabase() ->getBlockTable(pBlockTable, AcDb::kForRead); AcDbBlockTableRecord *pBlockTableRecord; pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord, AcDb::kForWrite); AcDbObjectId entId; pBlockTableRecord->appendAcDbEntity(entId, pEnt); pBlockTable->close(); pBlockTableRecord->close(); pEnt->close(); return entId; }
//二维多线段 static AcDbObjectId CreatePolyline(AcGePoint2dArray points, double width) { int numVertices = points.length(); AcDbPolyline *pPoly = new AcDbPolyline(numVertices); for (int i = 0; i < numVertices; i++) { pPoly->addVertexAt(i, points.at(i), 0, width, width); } AcDbObjectId polyId; polyId = PostToModelSpace(pPoly); return polyId; }
//添加文字 static AcDbObjectId CreateText(const AcGePoint3d& ptInsert,const char* text, AcDbObjectId style,double height, double rotation) { AcDbText *pText = new AcDbText(ptInsert, text, style, height,rotation); return PostToModelSpace(pText); }
//修改图层 static Acad::ErrorStatus ChangeLayer(AcDbObjectId entId,CString strLayerName) { AcDbEntity *pEntity; // 打开图形数据库中的对象 acdbOpenObject(pEntity, entId, AcDb::kForWrite); // 修改实体的图层 pEntity->setLayer(strLayerName); // 用完之后,及时关闭 pEntity->close(); return Acad::eOk; }
static Acad::ErrorStatus ChangeColor(AcDbObjectId entId, Adesk::UInt16 colorIndex) { AcDbEntity *pEntity; // 打开图形数据库中的对象 acdbOpenObject(pEntity, entId, AcDb::kForWrite); // 修改实体的颜色 pEntity->setColorIndex(colorIndex); // 用完之后,及时关闭 pEntity->close(); return Acad::eOk; }
} ;
望有大虾指点一二,先谢谢了 |
|