明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1891|回复: 3

请大虾帮忙看看我的程序的问题出现在哪里了?谢谢啊

[复制链接]
发表于 2009-9-8 21:50:00 | 显示全部楼层 |阅读模式
我是新手根据张帆的教程编写了个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;
}

} ;

望有大虾指点一二,先谢谢了
发表于 2009-9-8 23:49:00 | 显示全部楼层
一般这里的人比较懒,你还要说怎么个出错法,编译器或者cad报了什么样的错误,这样才能迅速解决你的问题
 楼主| 发表于 2009-9-9 08:49:00 | 显示全部楼层
本帖最后由 作者 于 2009-9-9 21:28:49 编辑

vs2002和cad都没有报错啊,只是我在代码里已经加了给多线段和文字更改图层的代码,可是实际运行出来后,多线段和图层还在0层,请帮忙开下,我这个程序错在哪里了,我找了好长时间都没找到错误的地方!先谢谢了!

发表于 2009-9-11 23:06:00 | 显示全部楼层
  1. //修改图层
  2. static Acad::ErrorStatus ChangeLayer(AcDbObjectId entId,CString strLayerName)
  3. {
  4. Acad::ErrorStatus es = Acad::eOk;
  5. AcDbEntity *pEntity;
  6. // 打开图形数据库中的对象
  7. if ( es = acdbOpenObject(pEntity, entId, AcDb::kForWrite) != Acad::eOk )
  8. {
  9. return es;
  10. };
  11. // 修改实体的图层
  12. pEntity->setLayer(strLayerName);
  13. // 用完之后,及时关闭
  14. pEntity->close();
  15. return Acad::eOk;
  16. }
  17. static Acad::ErrorStatus ChangeColor(AcDbObjectId entId, Adesk::UInt16 colorIndex)
  18. {
  19. //这里的修改同上!
  20. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 13:34 , Processed in 0.149659 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表