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