如何保存自定义类绘制的实体?
我自己开发了一个公路类,3条直线,可是绘制的图形无法保存,请高手指点!//类的头文件.h
#ifndef __ROADCLASS__H__
#define __ROADCLASS__H__
#include "dbents.h"
class Road
{
private:
AcGePoint3d g3Sp,g3Ep;//公路的起点和终点
double dBlg;//公路的突起值
double dWRd;//公路的宽度
int iClrRd;//公路颜色
AcDbObjectId idMiddle,idLift,idRight;//三条中线的id
public:
Road();
Road(AcGePoint3d g3StartPt,AcGePoint3d g3EndPt,
int iRoadColor,
double dBulge,double dWidthRoad);
~Road();
void drawRoad();
};//自定义类结束
#endif
//类的实现文件.cpp
#include "stdafx.h"
#include "croadclass.h"
#include "dbproxy.h"
/*************************************************************/
/***fast_changEntityColor 2k1 ***/
/*************************************************************/
/***功能描述:改变实体的颜色 ***/
/*************************************************************/
/*** 入口参数:AcDbEntity * x_pEntity ***/
/*** int x_color ***/
/***返回值:void ***/
/*** ***/
/*** ***/
/*************************************************************/
/***Programmed by Wang Zhiqin, 2001.08.01 ***/
/*************************************************************/
void fast_changEntityColor(AcDbEntity * x_pEntity,int x_color)
{
AcCmColor color;
color.setColorIndex(x_color);
x_pEntity->setColor(color);
}
/*************************************************************/
/*** fast_Entity_addToDb arx2000 ***/
/*************************************************************/
/***功能描述:把新创建的实体加入数据库 ***/
/*************************************************************/
/***入口参数:AcDbLine * pEntity, AcDbObjectId& id ***/
/***返回值: 0: 失败;1:成功; ***/
/*** int ***/
/*** ***/
/*************************************************************/
/***Programmed by wzq, 2001.06.01 ***/
/*************************************************************/
int fast_Entity_addToDb (AcDbEntity * pEntity,AcDbObjectId& id)
{
AcDbDatabase *pEntityDb;
AcDbBlockTable *pBlockTable;
AcDbBlockTableRecord *pBlockTableRecord;
// AcDbObjectId lineId;
// pEntityDb = acdbCurDwg(); //ObjectARX14的形式
pEntityDb = acdbHostApplicationServices()->workingDatabase();//ObjectARX2000的形式
// Get the model space Block Table Record
if(pEntityDb->getBlockTable(pBlockTable, AcDb::kForRead)!=Acad::eOk)
{
return 0;
}
if(pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord,
AcDb::kForWrite)!=Acad::eOk)
{
pBlockTable->close();
return 0;
}
pBlockTable->close();
if(pBlockTableRecord->appendAcDbEntity(id, pEntity)!=Acad::eOk)
{
pBlockTableRecord->close();
return 0;
}
pBlockTableRecord->close();
pEntity->close();
return 1;
}
/*
EDITION:2K
FUNCTION:fast_draw_2dpolyline
PARAMETER:起点集,突起值集
USE:绘制AcDb2dPolyline线,并加入到数据库;
RETURN:0:失败;1:成功;
AUTHOR:WANGQIN
*/
int fast_draw_2dpolyline(AcGePoint3dArray pts,
AcGeDoubleArray blgs,
int color,
double lw,
AcDbObjectId& id)
{
int ra;
AcDb2dPolyline * p2dpl=new AcDb2dPolyline(AcDb::k2dSimplePoly,pts,
0,Adesk::kFalse,lw,lw,&blgs);
fast_changEntityColor(p2dpl,color);
ra=fast_Entity_addToDb(p2dpl,id);
//p2dpl->close();
return ra;
}
Road::Road()
{
g3Sp.set(0.0,0.0,0.0);
g3Ep.set(300.0,0.0,0.0);
dBlg=0.0;
dWRd=32.0;
iClrRd=3;
idMiddle=0;
idLift=0;
idRight=0;
}
Road::Road(AcGePoint3d g3StartPt,AcGePoint3d g3EndPt,
int iRoadColor,
double dBulge,double dWidthRoad)
{
acutPrintf("\n执行构造函数2");
g3Sp=g3StartPt;
g3Ep=g3EndPt;
dBlg=dBulge;
dWRd=dWidthRoad;
iClrRd=iRoadColor;
idMiddle=0;
idLift=0;
idRight=0;
}
Road::~Road()
{
acutPrintf("\n执行析构函数");
}
void Road::drawRoad()
{
int iRz;
AcGeVector3d v3S2E=(g3Ep-g3Sp).normalize();//起点指向终点的单位矢量
AcGePoint3d g3SpLift,g3EpLift,g3SpRight,g3EpRight;//左边线和右边线的起点和终点
AcGePoint3dArray pts;
AcGeDoubleArray dBlgs;
//绘制中线
pts.append(g3Sp);
pts.append(g3Ep);
dBlgs.append(dBlg);
dBlgs.append(0);
iRz=fast_draw_2dpolyline(pts,dBlgs,iClrRd,0,idMiddle);
if(iRz==0)
{
return ;
}
pts.setLogicalLength(0);
dBlgs.setLogicalLength(0);
AcGeVector3d v3Tem1=v3S2E,v3Tem2=v3S2E;
//绘制边线1
g3SpLift=g3Sp+v3Tem1.perpVector()*dWRd;
g3EpLift=g3Ep+v3Tem1.perpVector()*dWRd;
pts.append(g3SpLift);
pts.append(g3EpLift);
dBlgs.append(dBlg);
dBlgs.append(0);
iRz=fast_draw_2dpolyline(pts,dBlgs,iClrRd,0,idLift);
if(iRz==0)
{
return ;
}
pts.setLogicalLength(0);
dBlgs.setLogicalLength(0);
//绘制边线2
g3SpRight=g3Sp-v3Tem2.perpVector()*dWRd;
g3EpRight=g3Ep-v3Tem2.perpVector()*dWRd;
pts.append(g3SpRight);
pts.append(g3EpRight);
dBlgs.append(dBlg);
dBlgs.append(0);
iRz=fast_draw_2dpolyline(pts,dBlgs,iClrRd,0,idLift);
if(iRz==0)
{
return ;
}
}
可能是别的地方有问题
我试了一下,可以运行,当然你的代码是有些问题,比如说int fast_Entity_addToDb (AcDbEntity * pEntity,AcDbObjectId& id)对pEntity的关闭处理得不好,AcDbEntity*同你调用的地方不统一;
另外,在AutoCAD里这不叫自定义类。
还有问题:
1、我绘制的的3条线的图形,用另存为命令,重新打开后,却没有图形。我想保存,该怎么办2、在AutoCAD中,这不叫自定义类,应该叫什么
自定义类
1、在AutoCAD中自定义类,除了C++概念外,另外一个意义是由自定义类产生的实体能被CAD所认识,也就是说,AutoCAD将不再把你的图形当线条处理,而是当公路来处理。2、不能另存不是这段代码的问题。
页:
[1]