注册 登录
明经CAD社区 返回首页

风树的个人空间 http://www.mjtd.com/?408117 [收藏] [复制] [分享] [RSS]

日志

(源码)生成一些基本cad实体函数

已有 1229 次阅读2013-11-21 19:35 |系统分类:应用


含生成文字、各类曲线、填充体、面域、矩形,线型等。因能用,没太多考虑好与不好的问题,尽量提供出错提示。
是在刚接触时借鉴、模仿、修改后形成的,给刚接触和想接触的同学一些参考例子。

//头文件///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma once
#ifndef _Cad_Line_Gon_h_ 
#define _Cad_Line_Gon_h_ 
#include "..\..\函数库\function_for_cad\function_for_cad.h"
#include "..\..\参数封装类\parameter_help\parameter_help.h"

using namespace xtcow;
namespace xtcow
{
class Cad_Line_Gon
{
public:
Cad_Line_Gon(void){};
~Cad_Line_Gon(void){};
static void FindAcadInstallPath(std::wstring &AcadInstallPath,std::wstring AcadRegiditPath=_T("SOFTWARE\\Autodesk\\AutoCAD\\R18.2\\ACAD-A001:804"));
static BOOL getLinetypeIdFromString(const std::wstring &str, AcDbObjectId& id);
static AcDb::LineWeight GetLineWidth(float iWidth);
//建立单行文字
static AcDbObjectId create_simple_text(AcGePoint3d pt,std::wstring text,text_parameter &simple_text_parameter_);
static void create_simple_text(std::vector<AcDbObjectId> &container,std::vector< std::pair<std::wstring,AcGePoint3d> >  &input_container,text_parameter &simple_text_parameter_);
static void create_simple_text(std::vector<AcDbObjectId> &container,std::vector< AcGePoint3d >  &input_container,text_parameter &simple_text_parameter_);
//建立多行文字
static AcDbObjectId createMutiText(const std::wstring &Text,AcDbObjectId TextStyle,const AcGePoint3d &BasePoint,
double texthight,double width,double angle=0,int color=0,
const std::wstring &layerName=_T("0"));
static void createMutiText_(std::vector<AcDbEntity *> &container,std::vector <AcGePoint3d>  &input_container,AcDbObjectId &TextStyle,
double texthight,double width,double angle=0,int color=0,
const std::wstring &layerName=_T("0"));
static void createMutiText_(std::vector< std::pair<std::wstring,AcGePoint3d> > PointID_to_Point3d,AcDbObjectId &TextStyle,
double texthight,double width,double angle,int color,
const std::wstring &layerName);//container返回对象指针
static void createMutiText_check(std::vector< std::pair<std::wstring,AcGePoint3d> > PointID_to_Point3d);
//直线类
static AcDbObjectId Draw_Line( double x1,double y1,double z1,  // 起点坐标
double x2,double y2,double z2  // 终点坐标
,int color=0);
static Acad::ErrorStatus Draw_Line(AcDbObjectId &lineId,AcGePoint3d startPt,AcGePoint3d endPt,int color,std::wstring Layer,std::wstring linetype);
static bool Draw_Line(std::vector< AcGePoint3d >&point3,std::vector< AcDbObjectId > &ObjectId,Adesk::UInt16 newColor=-1,std::wstring layer=_T("")); 
static AcDb2dPolyline* build_AcDb2dPolyline( AcGePoint3dArray &ptArr,bool IsClose=0);
//多段线类
static AcDb3dPolyline * Draw_3d_Triangle(AcGePoint3d &p1,AcGePoint3d &p2,AcGePoint3d &p3);
static Acad::ErrorStatus Draw_2dPolyline(AcDbObjectId& polylineId, AcGePoint3dArray &ptArr,
int Color=1, double Width=0,bool IsClose=0,std::wstring Layer=_T("0"),std::wstring linetype=_T(""));//画单条多段线
static Acad::ErrorStatus Draw_2dPolyline(AcDb2dPolyline* &polyline, AcGePoint3dArray &ptArr,
int Color=1, double Width=0,bool IsClose=0,std::wstring Layer=_T("0"),std::wstring linetype=_T(""));//画单条多段线
static Acad::ErrorStatus Draw_3d_Polyline(AcDbObjectId& polylineId, AcGePoint3dArray &ptArr,
int Color=1, double Width=0,std::wstring Layer=_T("0"),std::wstring linetype=_T(""));
static Acad::ErrorStatus Draw_3d_Polyline(AcDbObjectId& polylineId, std::vector< AcGePoint3d > &point_set,
int Color=1, double Width=0,std::wstring Layer=_T("0"),std::wstring linetype=_T(""));
static Acad::ErrorStatus Draw_3d_Polyline(AcDbObjectId& polylineId, std::vector< point_struct_3d<AcGePoint3d> >  &point_set,
int Color=1, double Width=0,std::wstring Layer=_T("0"),std::wstring linetype=_T(""));
static Acad::ErrorStatus Draw_Square(AcDbObjectIdArray& Id,std::vector < double > &x,std::vector < AcGePoint3d > &y,
int Color=1, double Width=0,std::wstring Layer=_T("0"),std::wstring linetype=_T(""));//画方形
static Acad::ErrorStatus Draw_Square(AcDbObjectIdArray& Id,std::vector < AcGePoint3d > &point,//左上角点
std::vector < double > &breadth,
std::vector < double > &high,
int Color=1, double Width=0,std::wstring Layer=_T("0"),std::wstring linetype=_T(""));//画方形
//样条曲线类
static Acad::ErrorStatus Draw_SplinePolyline(AcDbObjectId& polylineId, AcGePoint3dArray ptArr,
int Color, double Width,bool IsClose,std::wstring Layer,std::wstring linetype=_T(""));
//建立面域
static AcDbRegion* build_one_regions(AcDbVoidPtrArray& curveSegments);
//画水平矩形
static AcDb3dPolyline* build_rectang(AcGePoint3d left_bottom_point,AcGePoint3d right_top_point);
//填充类
static AcDbObjectId Draw_Hatch(
AcDbObjectId dbOId,
wchar_t cLayer[]=_T("0"), 
wchar_t cPattern[] =_T("SOLID"), 
int nColor =256, 
double dAngle =0.0, 
double dScale =1.0, 
AcDbDatabase * pDbDatab = acdbHostApplicationServices()->workingDatabase());
//dbOId为圆、椭圆、矩形、面域、闭合多段线等闭合、封闭的实体集
static Acad::ErrorStatus Draw_Hatch(
AcDbObjectIdArray &FanHuiId,
AcDbObjectIdArray &dbOId,
std::vector < std::wstring > &cLayer , 
std::vector < std::wstring >  &cPattern, 
std::vector < int > &nColor, 
std::vector < double > &dAngle, 
std::vector < double > &dScale, 
AcDbDatabase * pDbDatab= acdbHostApplicationServices()->workingDatabase());
//镜象
static Acad::ErrorStatus Draw_MirrorEntity( AcDbObjectId &Fanhui_Id,AcDbObjectId &Shuru_Id,
AcGePoint3d ptMirAxisStart,
AcGePoint3d ptMirAxisEnd,AcDbDatabase *Database= acdbHostApplicationServices()->workingDatabase());//单个id镜象
static Acad::ErrorStatus Draw_MirrorEntity( AcDbObjectIdArray &Fanhui_Id,AcDbObjectIdArray &Shuru_Id,
AcGePoint3d ptMirAxisStart,
AcGePoint3d ptMirAxisEnd,AcDbDatabase *Database= acdbHostApplicationServices()->workingDatabase());
   
};
}
#endif

//实现文件///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "Cad_Line_Gon.h"
#include "..\..\..\cad2012+arx\cad系统操作\Cad_BlockTableRecord_template\Cad_BlockTableRecord_template.h"
#include "..\..\..\cad2012+arx\cad系统操作\Cad_Lay_template\Cad_Lay_template.h"
#include "..\..\..\cad2012+arx\cad系统操作\Cad_Block\Cad_Block.h"
#include "..\..\..\cad2012+arx\函数库\function_for_cad\function_for_cad.h"//该文件内含一些常量及结构
using namespace xtcow;
namespace xtcow
{

//************************************************************************
//函数名称:FindAcadInstallPath
//功能描述:获得cad安装路径
//变量名:AcadInstallPath 变量类型:std::wstring &AcadInstallPath  变量说明:用来接受结果的变量
//变量名:AcadRegiditPath 变量类型:std::wstring     变量说明:在注册表里找的子键
//************************************************************************
void Cad_Line_Gon::FindAcadInstallPath(std::wstring &AcadInstallPath,std::wstring AcadRegiditPath)
{
//查找样式目录安装路径
AcadInstallPath.clear();
TCHAR AcadPath[255];
HKEY hKey;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,AcadRegiditPath.c_str(),0,KEY_QUERY_VALUE,&hKey)!=ERROR_SUCCESS)
{
acutPrintf(_T("\n设定的CAD注册路径不对"));
return ;
}
DWORD dwDataType=REG_SZ;
DWORD dwLength=255;
LONG lRet=RegQueryValueEx(hKey,TEXT("AcadLocation"),NULL,NULL,(LPBYTE)AcadPath,&dwLength);//TEXT("")内为键名
RegCloseKey(hKey);
if(lRet!=ERROR_SUCCESS)
{
acutPrintf(_T("\nRead failed"));
return ;
}
//-----查找完毕--------//
AcadInstallPath.append(AcadPath);
}
//************************************************************************
//函数名称:getLinetypeIdFromString
//功能描述:获得cad中以str名称的线型AcDbObjectId
//依靠函数:FindAcadInstallPath
//变量名:str 变量类型:   变量说明:以str名称的线型
//变量名:id 变量类型:AcDbObjectId& 变量说明:用来得到结果的变量
//************************************************************************
BOOL Cad_Line_Gon::getLinetypeIdFromString(const std::wstring &str, AcDbObjectId& id)
{
//----查找安装目录----//
std::wstring AcadInstallPath;
FindAcadInstallPath(AcadInstallPath);
std::wstring File=AcadInstallPath.append(_T("\\Support\\acad.lin"));
//-----查找完毕--------// 
AcDbLinetypeTable *pLinetypeTable=NULL;
acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pLinetypeTable, AcDb::kForRead);
Acad::ErrorStatus mess;
mess=pLinetypeTable->getAt(str.c_str(),id);
if(mess==Acad::eKeyNotFound||mess==Acad::ePermanentlyErased)
{
pLinetypeTable->close();
Acad::ErrorStatus error;
error=acdbLoadLineTypeFile(str.c_str(),File.c_str(),acdbHostApplicationServices()->workingDatabase());
if(error==Acad::eNullObjectPointer)
{
AcDbLinetypeTable *pLinetypeTable=NULL;
acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pLinetypeTable, AcDb::kForRead);
pLinetypeTable->getAt(_T("CONTINUOUS"), id);
pLinetypeTable->close();
return FALSE;
}
else if(error==Acad::eFileSystemErr)
{
acutPrintf(_T("the specified file cannot be opened"));
return FALSE;
}
else if(error==Acad::eUndefinedLineType)
{
AcDbLinetypeTable *pLinetypeTable=NULL;
acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pLinetypeTable, AcDb::kForRead);
acutPrintf(_T("the linetype name specified by ltname is not found in the file"));
pLinetypeTable->getAt(_T("CONTINUOUS"), id);
pLinetypeTable->close();
return TRUE;
}
AcDbLinetypeTable *pLinetypeTable=NULL;
acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pLinetypeTable, AcDb::kForRead);
pLinetypeTable->getAt(str.c_str(),id);
pLinetypeTable->close();
return TRUE;
}
pLinetypeTable->close();
return TRUE;
}

/*========================================================================
Name: 根据输入 得到对应的ACAD标准线宽. 
Params:  [iWidth]: 输入的线宽
使用方式:
wchar_t inputLayNamex[30];
acedGetString(0,_T("请输入线宽:"), inputLayNamex); 
_wtof(inputLayNamex);
int iWidth=GetLineWidth(_wtof(inputLayNamex) );
wchar_t text[10];
wsprintf(text,_T("%d\n"),iWidth);
acutPrintf(text);
===========================================================================*/
AcDb::LineWeight Cad_Line_Gon::GetLineWidth(float iWidth)
{
static AcDb::LineWeight LineWidth[27];//
static bool one;
if(one==0)
{
LineWidth[0]=AcDb::kLnWtByLwDefault;
LineWidth[1]=AcDb::kLnWtByBlock;
LineWidth[2]= AcDb::kLnWtByLayer ;
LineWidth[3]= AcDb::kLnWt000;
LineWidth[4]= AcDb::kLnWt005;
LineWidth[5]= AcDb::kLnWt009;
LineWidth[6]= AcDb::kLnWt013;
LineWidth[7]= AcDb::kLnWt015;
LineWidth[8]= AcDb::kLnWt018;
LineWidth[9]= AcDb::kLnWt020;
LineWidth[10]= AcDb::kLnWt025;
LineWidth[11]= AcDb::kLnWt030;
LineWidth[12]= AcDb::kLnWt035;
LineWidth[13]= AcDb::kLnWt040;
LineWidth[14]= AcDb::kLnWt050;
LineWidth[15]= AcDb::kLnWt053;
LineWidth[16]= AcDb::kLnWt060;
LineWidth[17]= AcDb::kLnWt070;
LineWidth[18]= AcDb::kLnWt080;
LineWidth[19]= AcDb::kLnWt090;
LineWidth[20]= AcDb::kLnWt100;
LineWidth[21]= AcDb::kLnWt106;
LineWidth[22]= AcDb::kLnWt120;
LineWidth[23]= AcDb::kLnWt140;
LineWidth[24]= AcDb::kLnWt158;
LineWidth[25]= AcDb::kLnWt200;
LineWidth[26]=AcDb::kLnWt211;
one=1;
}
/////////////////////////////////////////////////
int min=0;
int max=26;
int mid;
for(;min<max;)
{ mid=(max+min)>>1;
if(LineWidth[mid]<iWidth)
{
min=mid+1;
}
else//
{
max=mid;
}
}
if(LineWidth[min]==iWidth||min==26||min==0)return LineWidth[min];
else
return LineWidth[min-1];

}

//插入多行文本
/*使用例子:
AcGePoint3d nPt1;
nPt1.x=6;
nPt1.y=13;
nPt1.z=0;
bool shx=0;
wstring smallFontName=_T("宋体");
wstring layerName=_T("1");
int colour=60;
float textwidth=40;
float texthigh=12;
float textangle=0;
createMutiText(nPt1,kTextLeft,kTextBase,
_T("HAHAAAHA "),texthigh,textwidth, textangle,colour,
shx,layerName,smallFontName);
*/
AcDbObjectId Cad_Line_Gon::createMutiText(const std::wstring &Text,AcDbObjectId TextStyle,const AcGePoint3d &BasePoint,
double texthight,double width,double angle,int color,
const std::wstring &layerName)
{
AcDbMText *pMText=new AcDbMText();
if(pMText==NULL)
throw Acad::eOutOfMemory;
AcDbObjectId TextStyleId;
pMText->setLocation(BasePoint);//插入点
pMText->setContents(Text.c_str());//文字内容
pMText->setTextHeight(texthight);//文字高度
pMText->setWidth(width);//文字宽度
pMText->setTextStyle(TextStyleId);
pMText->setRotation(angle);
pMText->setLineSpacingFactor(0.8);
pMText->setColorIndex(color);
pMText->setLayer(layerName.c_str());
/////////////////////////////////////////////////////////////////////////////
AcDbObjectId MTextId;
MTextId=AddToModelSpace(pMText);////将pMText加入模型空间,得到一个AcDbObjectId
//////////////////////////////////////////////////////////////////
pMText->close();
return MTextId;
}
void Cad_Line_Gon::createMutiText_(std::vector< std::pair<std::wstring,AcGePoint3d> > PointID_to_Point3d,AcDbObjectId &TextStyle,
double texthight,double width,double angle,int color,
const std::wstring &layerName)//container返回对象指针
{
wchar_t text_[256];
std::vector< std::pair<std::wstring,AcGePoint3d> > ::iterator begin=PointID_to_Point3d.begin()+1;
for (int long long i=1;begin!=PointID_to_Point3d.end();begin++,i++)
{
AcDbMText *pMText=new AcDbMText();
AcDbObjectId TextStyleId;
pMText->setLocation((*begin).second);//插入点
swprintf(text_,_T("%d号孔"),i);
pMText->setContents((*begin).first.c_str());//文字内容
pMText->setTextHeight(texthight);//文字高度
pMText->setWidth(width);//文字宽度
pMText->setTextStyle(TextStyleId);
pMText->setRotation(angle);
pMText->setLineSpacingFactor(0.8);
pMText->setColorIndex(color);
pMText->setLayer(layerName.c_str());
AddToModelSpace(pMText);
pMText->close();
//container.push_back(pMText);
}
}
void Cad_Line_Gon::createMutiText_check(std::vector< std::pair<std::wstring,AcGePoint3d> > PointID_to_Point3d)
{
AcGePoint3d nPt1;
nPt1.x=6;
nPt1.y=13;
nPt1.z=0;
bool shx=0;
std::wstring smallFontName=_T("宋体");
std::wstring layerName=_T("1");
int color=60;
float width=40;
float texthight=12;
float angle=0;
AcDbObjectId uuu=text_parameter::CreateTextStyle(_T("textstyle_name"),width, smallFontName,0);
//
wchar_t text_[256];
std::vector< std::pair<std::wstring,AcGePoint3d> > ::iterator begin=PointID_to_Point3d.begin()+1;
for (int long long i=1;begin!=PointID_to_Point3d.end();begin++,i++)
{
AcDbMText *pMText=new AcDbMText();
AcDbObjectId TextStyleId;
pMText->setLocation((*begin).second);//插入点
swprintf(text_,_T("%d号孔"),i);
pMText->setContents((*begin).first.c_str());//文字内容
pMText->setTextHeight(texthight);//文字高度
pMText->setWidth(width);//文字宽度
pMText->setTextStyle(TextStyleId);
pMText->setRotation(angle);
pMText->setLineSpacingFactor(0.8);
pMText->setColorIndex(color);
pMText->setLayer(layerName.c_str());
AddToModelSpace(pMText);
pMText->close();
//container.push_back(pMText);
}
}
void Cad_Line_Gon::createMutiText_(std::vector<AcDbEntity *> &container,std::vector <AcGePoint3d>  &input_container,AcDbObjectId &TextStyle,
double texthight,double width,double angle,int color,
const std::wstring &layerName)//container返回对象指针
{

wchar_t text_[256];
container.reserve(input_container.size());
std::vector <AcGePoint3d>::iterator begin=input_container.begin();
for (int long long i=1;begin!=input_container.end();begin++,i++)
{
AcDbMText *pMText=new AcDbMText();
AcDbObjectId TextStyleId;
pMText->setLocation(*begin);//插入点
swprintf(text_,_T("%d号"),i);
pMText->setContents(text_);//文字内容
pMText->setTextHeight(texthight);//文字高度
pMText->setWidth(width);//文字宽度
pMText->setTextStyle(TextStyleId);
pMText->setRotation(angle);
pMText->setLineSpacingFactor(0.8);
pMText->setColorIndex(color);
pMText->setLayer(layerName.c_str());
container.push_back(pMText);
}
}
//插入单行文本
AcDbObjectId Cad_Line_Gon::create_simple_text(AcGePoint3d pt,std::wstring text,text_parameter &simple_text_parameter_)
{
AcDbText *pText=NULL;
pText=new AcDbText;
if(pText==NULL)
throw Acad::eOutOfMemory;
pText->setTextStyle(simple_text_parameter_.get_text_style());
pText->setHorizontalMode(simple_text_parameter_.hMode);
pText->setVerticalMode(simple_text_parameter_.vMode);
pText->setTextString(text.c_str());
pText->setHeight(simple_text_parameter_.hight);
pText->setColorIndex(simple_text_parameter_.color);
pText->setRotation(simple_text_parameter_.rotation);
//pText->setWidthFactor(widthFactor);
pText->setAlignmentPoint(pt);
if(simple_text_parameter_.layerName!=_T(""))
pText->setLayer(simple_text_parameter_.layerName.c_str());
AcDbObjectId textId=AddToModelSpace( pText);
pText->close();
return textId;
}
//插入单行文本
void Cad_Line_Gon::create_simple_text(std::vector<AcDbObjectId> &container,std::vector< std::pair<std::wstring,AcGePoint3d> >  &input_container,text_parameter &simple_text_parameter_)
{
std::vector< std::pair<std::wstring,AcGePoint3d> >::const_iterator begin=input_container.begin();
container.clear();
for (;begin!=input_container.end();begin++)
{
AcDbText *pText=NULL;
pText=new AcDbText;
pText->setTextStyle(simple_text_parameter_.get_text_style());
pText->setHorizontalMode(simple_text_parameter_.hMode);
pText->setVerticalMode(simple_text_parameter_.vMode);
pText->setTextString((simple_text_parameter_.text+double_to_wstring((((int)StrToFloatW((*begin).first.c_str())+simple_text_parameter_.count-1)),0)).c_str());
pText->setHeight(simple_text_parameter_.hight);
pText->setColorIndex(simple_text_parameter_.color);
pText->setRotation(simple_text_parameter_.rotation);
pText->setAlignmentPoint((*begin).second);
pText->setLayer(simple_text_parameter_.layerName.c_str());
AcDbObjectId textId=AddToModelSpace( pText);
container.push_back(textId);
pText->close();
}
}

//插入单行文本
void Cad_Line_Gon::create_simple_text(std::vector<AcDbObjectId> &container,std::vector< AcGePoint3d >  &input_container,text_parameter &simple_text_parameter_)
{
std::vector< AcGePoint3d >::const_iterator begin=input_container.begin();
container.clear();
Acad::ErrorStatus es=Acad::eOk;
if (!has_the_same_lay(simple_text_parameter_.layerName.c_str(),NULL))
{
creat_new_layer(simple_text_parameter_.layerName.c_str(),NULL);
}
for (int i=1;begin!=input_container.end();begin++,i++)
{
AcDbText *pText=NULL;
pText=new AcDbText;
es=pText->setTextStyle(simple_text_parameter_.get_text_style());
es=pText->setHorizontalMode(simple_text_parameter_.hMode);
es=pText->setVerticalMode(simple_text_parameter_.vMode);
es=pText->setTextString((simple_text_parameter_.text+int_to_wstring(i+simple_text_parameter_.count-1)).c_str());
es=pText->setHeight(simple_text_parameter_.hight);
es=pText->setColorIndex(simple_text_parameter_.color);
es=pText->setRotation(simple_text_parameter_.rotation);
es=pText->setAlignmentPoint(*begin);
es=pText->setLayer(simple_text_parameter_.layerName.c_str());
container.push_back(AddToModelSpace( pText));
pText->close();
}
}
AcDbObjectId Cad_Line_Gon::Draw_Line( double x1,double y1,double z1,  // 起点坐标
double x2,double y2,double z2  // 终点坐标
,int color)                   // 直线所在图层
{
AcGePoint3d StartPt(x1,y1,z1); // 起点
AcGePoint3d EndPt(x2,y2,z2); // 终点
AcDbLine *pLine = new AcDbLine(StartPt,EndPt);
// 获得当前图形数据库的符号表
AcDbBlockTable *pBlockTable=NULL;
acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pBlockTable,
AcDb::kForRead);
// 获得符号表中的模型空间块表记录指针,用于添加对象
AcDbBlockTableRecord *pBlockTableRecord=NULL;
pBlockTable->getAt(ACDB_MODEL_SPACE,pBlockTableRecord,AcDb::kForWrite);
pBlockTable->close();
// 将直线添加到模型空间块表记录中
AcDbObjectId lineId;
pLine-> setColorIndex(color);
//pLine->setLayer(layer.c_str(),Adesk::kTrue); // 设置直线所在图层
pBlockTableRecord->appendAcDbEntity(lineId,pLine);
// 关闭块表记录指针和直线指针
pBlockTableRecord->close();
pLine->close();
// 返回直线ID号
return lineId;
}
//
//功能:在指定图层上根据点矩阵新建一条多段线
//参数:point3 输入坐标向量;返回的ObjectId向量;layer直线所在图层;newColor线色
//返回:直线ID
// vector< AcGePoint3d > point3;
// point3.push_back(AcGePoint3d(3,3,3));
// point3.push_back(AcGePoint3d(7,7,7));
// point3.push_back(AcGePoint3d(13,13,13));
// vector< AcDbObjectId > ObjectId;
// std::wstring layer=_T("1");
// CreateLine(point3,ObjectId,50,layer);
bool Cad_Line_Gon::Draw_Line(std::vector< AcGePoint3d >&point3,std::vector< AcDbObjectId > &ObjectId,Adesk::UInt16 newColor,std::wstring layer) 
{
// 获得当前图形数据库的符号表
AcDbBlockTable *pBlockTable=NULL;
acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pBlockTable,AcDb::kForRead);
// 获得符号表中的模型空间块表记录指针,用于添加对象
AcDbBlockTableRecord *pBlockTableRecord=NULL;
pBlockTable->getAt(ACDB_MODEL_SPACE,pBlockTableRecord,AcDb::kForWrite);
pBlockTable->close();
// 将直线添加到模型空间块表记录中
ObjectId.clear();
std::vector<AcGePoint3d>::iterator iter=point3.begin(), end=point3.end();
for(;(iter+1)!=end;++iter)
{
AcDbLine *pLine = new AcDbLine(*iter,*(iter+1));
if(layer.c_str()!=_T(""))
{
pLine->setLayer(layer.c_str(),Adesk::kTrue); // 设置直线所在图层
}
if(newColor!=-1)
{
pLine->setColorIndex(newColor); // 设置直线颜色
}
AcDbObjectId lineId;
pBlockTableRecord->appendAcDbEntity(lineId,pLine);
ObjectId.push_back(lineId);
pLine->close();
}
// 关闭块表记录指针和直线指针
pBlockTableRecord->close();
// 返回直线ID号
return 1;
}
//
//功能:在指定图层上根据点矩阵新建一条线(和上面的重复:)
//参数:point3 输入坐标向量;返回的ObjectId向量;layer直线所在图层;newColor线色
//返回:直线ID
// vector< AcGePoint3d > point3;
// point3.push_back(AcGePoint3d(3,3,3));
// point3.push_back(AcGePoint3d(7,7,7));
// point3.push_back(AcGePoint3d(13,13,13));
// vector< AcDbObjectId > ObjectId;
// std::wstring layer=_T("1");
// CreateLine(point3,ObjectId,50,layer);
Acad::ErrorStatus Cad_Line_Gon::Draw_Line(AcDbObjectId &lineId,AcGePoint3d startPt,AcGePoint3d endPt,int color,std::wstring Layer,std::wstring linetype)
{
Acad::ErrorStatus es=Acad::eOk;

AcDbLine *pLine = new AcDbLine(startPt, endPt);
if((es=pLine->setColorIndex(color))!=Acad::eOk)
{
pLine->close();return es;
}
if(Layer.size()!=0)
{
if(pLine->setLayer(Layer.c_str())==Acad::eKeyNotFound \
||pLine->setLayer(Layer.c_str())==Acad::eDeletedEntry )
{
AcCmColor color1;
color1.setColorIndex(0);
creat_new_layer(Layer.c_str(),NULL);//建立新图层
if((es=pLine->setLayer(Layer.c_str()))!=Acad::eOk)
{
pLine->close();return es;
}
}
}
if(linetype.size()!=0)
{
AcDbObjectId lineTypeId;
if(getLinetypeIdFromString(linetype,lineTypeId))
{
if((es=pLine->setLinetype(lineTypeId))!=Acad::eOk)
{
pLine->close();return es;
}
if((es=pLine->setLinetypeScale(1))!=Acad::eOk)
{
pLine->close();return es;
}
}
}
lineId=AddToModelSpace(pLine);
pLine->close();
return es;
}
Acad::ErrorStatus Cad_Line_Gon::Draw_3d_Polyline(AcDbObjectId& polylineId, AcGePoint3dArray &ptArr,
int Color, double Width,std::wstring Layer,std::wstring linetype)
{

polylineId =AcDbObjectId::kNull;

Acad::ErrorStatus es=Acad::eOk;
AcDb3dPolyline *pNewPline=NULL;
pNewPline=new AcDb3dPolyline(AcDb::k3dSimplePoly,ptArr);//建立闭合多段线对象
if(pNewPline==NULL)
return Acad::eOutOfMemory;//new失败,OutOfMemory

if((es=pNewPline->setColorIndex(Color))!=Acad::eOk)//new成功后的各种设置:设置颜色
{
pNewPline->close();return es;
}

if(pNewPline->setLayer(Layer.c_str())==Acad::eKeyNotFound ||pNewPline->setLayer(Layer.c_str())==Acad::eDeletedEntry )//设置图层颜色时碰到图层不存在或。。
{
creat_new_layer(Layer.c_str(),NULL);//建立新图层
if((es=pNewPline->setLayer(Layer.c_str()))!=Acad::eOk)//再次设置图层颜色
{ //设置失败,没办法了
pNewPline->close();
return es;
}
}

if(linetype.size()!=0)//设置线型,空就不设定线型
{
AcDbObjectId lineTypeId;
if(getLinetypeIdFromString(linetype,lineTypeId))//获得线型id成功
{
if((es=pNewPline->setLinetype(lineTypeId))!=Acad::eOk)
{
acutPrintf(_T("线型设置出现问题,操作停止"));
pNewPline->close();return es;
}
}

if((es=pNewPline->setLinetypeScale(1))!=Acad::eOk)
{
acutPrintf(_T("线型Scale设置出现问题,操作停止"));
pNewPline->close();
return es;
}
}
////////////////////////////////////////////////////////////
AcDbBlockTableRecord *pBlockTableRecord=NULL;
es=Cad_BlockTableRecord<AcDbBlockTableRecord,std::vector<AcDbBlockTableRecord> >::OpenSpace(pBlockTableRecord,acdbHostApplicationServices()->workingDatabase()); 
if(es!=Acad::eOk)
{
acutPrintf(_T("\n 获得pBlockTableRecord操作未能完成"));
pNewPline->close();
return es;
}
es=pBlockTableRecord->appendAcDbEntity(polylineId,pNewPline);
if(es!=Acad::eOk)//未能正确加入pBlockTableRecord
{
pNewPline->close();
Acad::ErrorStatus es2=pBlockTableRecord->close();
if (es2!=Acad::eOk)
{
acutPrintf(_T("\n关闭模型空间块表记录失败,错误码为:%s"),
acadErrorStatusText(es2));
}
return es;
}
else
{
es=pNewPline->close();
if(es!=Acad::eOk)
{
acutPrintf(_T("\n应用程序X关闭实体失败,错误码为:%d"),
acadErrorStatusText(es));
}

es=pBlockTableRecord->close();
if(es!=Acad::eOk)
{
acutPrintf(_T("\n关闭模型空间块表记录失败,错误码为:%s"),
acadErrorStatusText(es));
}

}
return es;
}
Acad::ErrorStatus Cad_Line_Gon::Draw_3d_Polyline(AcDbObjectId& polylineId, std::vector< AcGePoint3d > &point_set,
int Color, double Width,std::wstring Layer,std::wstring linetype)
{

polylineId =AcDbObjectId::kNull;
if (point_set.size()<2)
{
return Acad::eInvalidInput; 
}
AcGePoint3dArray ptArr;
for (int i=0;i<point_set.size();i++)
{
ptArr.append(point_set[i]);
}
return Draw_3d_Polyline(polylineId,ptArr,Color, Width,Layer,linetype);
}
Acad::ErrorStatus Cad_Line_Gon::Draw_3d_Polyline(AcDbObjectId& polylineId, std::vector< point_struct_3d<AcGePoint3d> > &point_set,
int Color, double Width,std::wstring Layer,std::wstring linetype)
{

polylineId =AcDbObjectId::kNull;
if (point_set.size()<2)
{
return Acad::eInvalidInput; 
}
AcGePoint3dArray ptArr;
for (int i=0;i<point_set.size();i++)
{
ptArr.append(AcGePoint3d(point_set[i].x,point_set[i].y,point_set[i].z));
}
return Draw_3d_Polyline(polylineId,ptArr,Color, Width,Layer,linetype);
}
AcDb3dPolyline * Cad_Line_Gon::Draw_3d_Triangle(AcGePoint3d &p1,AcGePoint3d &p2,AcGePoint3d &p3)
{
AcGePoint3dArray ptArr;
ptArr.append(p1);
ptArr.append(p2);
ptArr.append(p3);
ptArr.append(p1);
//
AcDb3dPolyline *pNewPline=NULL;
pNewPline=new AcDb3dPolyline(AcDb::k3dSimplePoly,ptArr);//建立闭合多段线对象
if(pNewPline==NULL)
return 0;//new失败
////////////////////////////////////////////////////////////
return pNewPline;
}
//
//函数名称:DrawPolyline
//功能描述:画多段线
//依靠函数:getLinetypeIdFromString
//变量名:polylineId 变量类型:AcDbObjectId&     变量说明:得到的多段线id
//变量名:ptArr 变量类型:AcGePoint3dArray& 变量说明:输入的端点向量
//变量名:Layer 变量类型: 变量说明:多段线所在的图层
//变量名:linetype 变量类型: 变量说明:线型,输入空的值或者系统不存在的值将用实线表示
//设置多段线的四个矢量
/*例子
AcGePoint3dArray ptArr;
ptArr.setLogicalLength(4);
for (int i = 0; i < 4; i++) 
 {
 ptArr[i].set((double)(i/2), (double)(i%2), 0.0);
 }
AcDbObjectId  polylineId;
int ColorIndex=50;
double Width=2;
bool IsClose=0;
wstring LayerName=_T("HAHA号");

DrawPolyline(polylineId, ptArr,ColorIndex,Width,IsClose,LayerName,_T("DASHED"));
*/
Acad::ErrorStatus Cad_Line_Gon::Draw_2dPolyline(AcDbObjectId& polylineId, AcGePoint3dArray &ptArr,
int Color, double Width,bool IsClose,std::wstring Layer,std::wstring linetype)
{
polylineId =AcDbObjectId::kNull;

Acad::ErrorStatus es=Acad::eOk;
AcDb2dPolyline *pNewPline=NULL;
if(IsClose)
{ //动态创建一个AcDb2dPolyline对象,在ptArr中给出四个顶点位置
//缺省的线宽为0
pNewPline=new AcDb2dPolyline(AcDb::k2dSimplePoly,ptArr,0,Adesk::kTrue,Width,Width);//建立闭合多段线对象
}
else
{
pNewPline=new AcDb2dPolyline(AcDb::k2dSimplePoly,ptArr,0,Adesk::kFalse,Width,Width);//建立非闭合多段线对象
}

if(pNewPline==NULL)
return Acad::eOutOfMemory;//new失败,OutOfMemory
pNewPline->setElevation(0);
if((es=pNewPline->setColorIndex(Color))!=Acad::eOk)//new成功后的各种设置:设置颜色
{
pNewPline->close();
return es;
}

if(pNewPline->setLayer(Layer.c_str())==Acad::eKeyNotFound ||pNewPline->setLayer(Layer.c_str())==Acad::eDeletedEntry )//设置图层颜色时碰到图层不存在或。。
{
creat_new_layer(Layer.c_str(),NULL);//建立新图层
if((es=pNewPline->setLayer(Layer.c_str()))!=Acad::eOk)//再次设置图层颜色
{ //设置失败,没办法了
pNewPline->close();
return es;
}
}

if(linetype.size()!=0)//设置线型,空就不设定线型
{
AcDbObjectId lineTypeId;
if(getLinetypeIdFromString(linetype,lineTypeId))//获得线型id成功
{
if((es=pNewPline->setLinetype(lineTypeId))!=Acad::eOk)
{
acutPrintf(_T("\n线型设置出现问题,操作停止"));
pNewPline->close();
return es;
}
}

if((es=pNewPline->setLinetypeScale(1))!=Acad::eOk)
{
acutPrintf(_T("\n线型Scale设置出现问题,操作停止"));
pNewPline->close();
return es;
}
}
if(!pNewPline->isLinetypeGenerationOn())//如果多段线的线型生成打开,则返回Adesk::kTrue,
{
if((es=pNewPline->setLinetypeGenerationOn())!=Acad::eOk)
{
acutPrintf(_T("\nsetLinetypeGenerationOn出现问题,操作停止"));
pNewPline->close();
return es;
}
}
////////////////////////////////////////////////////////////
AcDbBlockTableRecord *pBlockTableRecord=NULL;
es=Cad_BlockTableRecord<AcDbBlockTableRecord,std::vector<AcDbBlockTableRecord> >::OpenSpace(pBlockTableRecord,acdbHostApplicationServices()->workingDatabase()); 
if(es!=Acad::eOk)
{
acutPrintf(_T("\n 获得pBlockTableRecord操作未能完成"));
pNewPline->close();
return es;
}
es=pBlockTableRecord->appendAcDbEntity(polylineId,pNewPline);
if(es!=Acad::eOk)//未能正确加入pBlockTableRecord
{
pNewPline->close();
Acad::ErrorStatus es2=pBlockTableRecord->close();
if (es2!=Acad::eOk)
{
acutPrintf(_T("\n关闭模型空间块表记录失败,错误码为:%s"),
acadErrorStatusText(es2));
}
return es;
}
else
{
es=pNewPline->close();
if(es!=Acad::eOk)
{
acutPrintf(_T("\n应用程序X关闭实体失败,错误码为:%d"),
acadErrorStatusText(es));
}

es=pBlockTableRecord->close();
if(es!=Acad::eOk)
{
acutPrintf(_T("\n关闭模型空间块表记录失败,错误码为:%s"),
acadErrorStatusText(es));
}

}
return es;
}
Acad::ErrorStatus Cad_Line_Gon::Draw_2dPolyline(AcDb2dPolyline* &polyline, AcGePoint3dArray &ptArr,
int Color, double Width,bool IsClose,std::wstring Layer,std::wstring linetype)
{
Acad::ErrorStatus es=Acad::eOk;
if(IsClose)
{ //动态创建一个AcDb2dPolyline对象,在ptArr中给出四个顶点位置
//缺省的线宽为0
polyline=new AcDb2dPolyline(AcDb::k2dSimplePoly,ptArr,0,Adesk::kTrue,Width,Width);//建立闭合多段线对象
}
else
{
polyline=new AcDb2dPolyline(AcDb::k2dSimplePoly,ptArr,0,Adesk::kFalse,Width,Width);//建立非闭合多段线对象
}

if(polyline==NULL)
return Acad::eOutOfMemory;//new失败,OutOfMemory

if((es=polyline->setColorIndex(Color))!=Acad::eOk)//new成功后的各种设置:设置颜色
{
polyline->close();
return es;
}

if(polyline->setLayer(Layer.c_str())==Acad::eKeyNotFound ||polyline->setLayer(Layer.c_str())==Acad::eDeletedEntry )//设置图层颜色时碰到图层不存在或。。
{
creat_new_layer(Layer.c_str(),NULL);//建立新图层
if((es=polyline->setLayer(Layer.c_str()))!=Acad::eOk)//再次设置图层颜色
{ //设置失败,没办法了
polyline->close();
return es;
}
}

if(linetype.size()!=0)//设置线型,空就不设定线型
{
AcDbObjectId lineTypeId;
if(getLinetypeIdFromString(linetype,lineTypeId))//获得线型id成功
{
if((es=polyline->setLinetype(lineTypeId))!=Acad::eOk)
{
acutPrintf(_T("\n线型设置出现问题,操作停止"));
polyline->close();return es;
}
}

if((es=polyline->setLinetypeScale(1))!=Acad::eOk)
{
acutPrintf(_T("\n线型Scale设置出现问题,操作停止"));
polyline->close();return es;
}
}
if(!polyline->isLinetypeGenerationOn())//如果多段线的线型生成打开,则返回Adesk::kTrue,
{
if((es=polyline->setLinetypeGenerationOn())!=Acad::eOk)
{
polyline->close();
return es;
}
}
return es;
}
AcDb2dPolyline* Cad_Line_Gon::build_AcDb2dPolyline( AcGePoint3dArray &ptArr,bool IsClose)
{
AcDb2dPolyline* polyline=0;
if(IsClose)
{ //动态创建一个AcDb2dPolyline对象,在ptArr中给出四个顶点位置
//缺省的线宽为0
polyline=new AcDb2dPolyline(AcDb::k2dSimplePoly,ptArr,0,Adesk::kTrue);//建立闭合多段线对象
}
else
{
polyline=new AcDb2dPolyline(AcDb::k2dSimplePoly,ptArr,0,Adesk::kFalse);//建立非闭合多段线对象
}
return polyline;
}
//************************************************************************
//函数名称:DrawSquare
//功能描述:根据输入数据画连续的正方形,如画钻孔柱状各地层(注意所画的图形不是连续的是不能用该函数,要改动一下)
//依靠函数:getLinetypeIdFromString
//变量名:Id 变量类型:AcDbObjectIdArray&     变量说明:输入空的AcDbObjectIdArray来接受返回数据
//变量名:x 变量类型:std::vector < double > & 变量说明:宽度
//变量名:y 变量类型:std::vector < AcGePoint2d > & 变量说明:所要画的连续正方形左边的点序列
//变量名:linetype 变量类型: 变量说明:线型,输入空的值或者系统不存在的值将用实线表示
//************************************************************************
Acad::ErrorStatus Cad_Line_Gon::Draw_Square(AcDbObjectIdArray& Id,std::vector < double > &x,std::vector < AcGePoint3d > &y,
int Color, double Width,std::wstring Layer,std::wstring linetype)
{

Id.removeAll();//清空矩阵
////////////////////////////////////////////////////////////加入空间
Acad::ErrorStatus es=Acad::eOk;
AcDbBlockTableRecord *pBlockTableRecord=NULL;
es=Cad_BlockTableRecord<AcDbBlockTableRecord,std::vector<AcDbBlockTableRecord> >::OpenSpace(pBlockTableRecord,acdbHostApplicationServices()->workingDatabase()); 
if(es!=Acad::eOk)
{
acutPrintf(_T(" \n获得pBlockTableRecord操作未能完成"));
return es;
}
std::vector < AcGePoint3d >::iterator   it_y;
std::vector < double > ::iterator   it_x;
it_y=y.begin();
it_x=x.begin();
AcGePoint2dArray ptArr;
AcDb2dPolyline *NewSquare=NULL;
for (;it_y!=(y.end()-1);it_y++,it_x++)//直到运行到倒数第二个
{

NewSquare=new AcDb2dPolyline();//建立闭合多段线对象
if(NewSquare==NULL)
return Acad::eOutOfMemory;//new失败,OutOfMemory
//变换矩阵
AcGeMatrix3d M;
M.setToTranslation(AcGeVector3d(*it_x,0,0));

AcDb2dVertex *point1=new AcDb2dVertex(*it_y);
AcDb2dVertex *point2=new AcDb2dVertex(*(it_y+1));
AcGePoint3d temp1=*(it_y+1);
temp1.transformBy(M);
AcDb2dVertex *point3=new AcDb2dVertex(temp1);

AcGePoint3d temp2=*(it_y);
temp2.transformBy(M);
AcDb2dVertex *point4=new AcDb2dVertex(temp2);

AcDbObjectId o1;
es=NewSquare->appendVertex(point1);

AcDbObjectId o2;
es=NewSquare->appendVertex(point2);
AcDbObjectId o3;
es=NewSquare->appendVertex(point3);
AcDbObjectId o4;
es=NewSquare->appendVertex(point4);


//设置属性
NewSquare->setVisibility(AcDb::kVisible);
#ifdef higher_than_cad_arx_2012
NewSquare->setClosed(1);
es=NewSquare->setConstantWidth(Width);
#endif
#ifdef cad_arx_2010
NewSquare->makeClosed();
es=NewSquare->setDefaultStartWidth(GetLineWidth(Width));
es=NewSquare->setDefaultStartWidth(GetLineWidth(Width));
es=NewSquare->setDefaultEndWidth(Width);
#endif
#ifdef lower_than_cad_arx_2010
NewSquare->makeClosed();
es=NewSquare->setDefaultStartWidth(GetLineWidth(Width));
es=NewSquare->setDefaultStartWidth(GetLineWidth(Width));
es=NewSquare->setDefaultEndWidth(Width);
#endif
es=NewSquare->setColorIndex(Color);
if(es!=Acad::eOk)
{
acutPrintf(_T("\n设置对象参数出现问题,操作停止"));
NewSquare->close();
return es;
}
//
es=NewSquare->setLayer(Layer.c_str());
if(es==Acad::eKeyNotFound ||es==Acad::eDeletedEntry )//设置图层颜色时碰到图层不存在或。。
{
acutPrintf(_T("\n图层设置出现问题,操作停止"));
NewSquare->close();
return es;
}
//
if(linetype.size()!=0)//设置线型,空就不设定线型
{
AcDbObjectId lineTypeId;
if(getLinetypeIdFromString(linetype,lineTypeId))//获得线型id成功
{
if((es=NewSquare->setLinetype(lineTypeId))!=Acad::eOk)
{
acutPrintf(_T("\n线型设置出现问题,操作停止"));
NewSquare->close();
return es;
}
}

if((es=NewSquare->setLinetypeScale(1))!=Acad::eOk)
{
acutPrintf(_T("\n线型Scale设置出现问题,操作停止"));
NewSquare->close();
return es;
}

}
if(!NewSquare->isLinetypeGenerationOn())
{
if((es=NewSquare->setLinetypeGenerationOn())!=Acad::eOk)
{
acutPrintf(_T("\n线型Scale设置出现问题,操作停止"));
NewSquare->close();
return es;
}
}
AcDbObjectId Id1;
es=pBlockTableRecord->appendAcDbEntity(Id1,NewSquare);
if(es!=Acad::eOk)//未能正确加入pBlockTableRecord
{
NewSquare->close();
Acad::ErrorStatus es2=pBlockTableRecord->close();
if (es2!=Acad::eOk)
{
acutPrintf(_T("\n关闭模型空间块表记录失败,错误码为:%s"),
acadErrorStatusText(es2));
}
return es;
}
else
{ //加入成功
Id.append(Id1);
es=NewSquare->close();//释放资源
if(es!=Acad::eOk)
{
acutPrintf(_T("\n应用程序X关闭实体失败,错误码为:%d"),
acadErrorStatusText(es));
}

}
point1->close();
point2->close();
point3->close();
point4->close();

}

es=pBlockTableRecord->close();
if(es!=Acad::eOk)
{
acutPrintf(_T("\n关闭模型空间块表记录失败,错误码为:%s"),
acadErrorStatusText(es));
}
return es;
}

//point为各个代画矩形的左上角,
Acad::ErrorStatus Cad_Line_Gon::Draw_Square(AcDbObjectIdArray& Id,std::vector < AcGePoint3d > &point,
std::vector < double > &breadth,
std::vector < double > &high,
int Color, double Width,std::wstring Layer,std::wstring linetype)
{

Id.removeAll();//清空矩阵
////////////////////////////////////////////////////////////加入空间
Acad::ErrorStatus es=Acad::eOk;
AcDbBlockTableRecord *pBlockTableRecord=NULL;//要释放资源
es=Cad_BlockTableRecord<AcDbBlockTableRecord,std::vector<AcDbBlockTableRecord> >::OpenSpace(pBlockTableRecord,acdbHostApplicationServices()->workingDatabase()); 
if(es!=Acad::eOk)
{
acutPrintf(_T(" \n获得pBlockTableRecord操作未能完成"));
return es;
}
std::vector < AcGePoint3d >::iterator   point_=point.begin();
std::vector < double > ::iterator   breadth_=breadth.begin();
std::vector < double > ::iterator   high_=high.begin();
AcDb2dPolyline *NewSquare=NULL;
for (;point_!=point.end();point_++,breadth_++,high_++)//直到运行到倒数第二个
{

NewSquare=new AcDb2dPolyline();//建立闭合多段线对象
if(NewSquare==NULL)
{
return Acad::eOutOfMemory;//new失败,OutOfMemory

}
//变换矩阵
AcGeMatrix3d M;
M.setToTranslation(AcGeVector3d(*breadth_,0,0));

AcDb2dVertex *point1=new AcDb2dVertex(*point_);
AcDb2dVertex *point2=new AcDb2dVertex(AcGePoint3d((*point_).x,(*point_).y-*high_,0));
AcGePoint3d temp1=(AcGePoint3d((*point_).x,(*point_).y-*high_,0));
temp1.transformBy(M);
AcDb2dVertex *point3=new AcDb2dVertex(temp1);

AcGePoint3d temp2=*point_;
temp2.transformBy(M);
AcDb2dVertex *point4=new AcDb2dVertex(temp2);

es=NewSquare->appendVertex(point1);

es=NewSquare->appendVertex(point2);

es=NewSquare->appendVertex(point3);

es=NewSquare->appendVertex(point4);


//设置属性
NewSquare->setVisibility(AcDb::kVisible);

#ifdef higher_than_cad_arx_2012
NewSquare->setClosed(1); 
#endif
#ifdef cad_arx_2010
NewSquare->makeClosed();
#endif
#ifdef lower_than_cad_arx_2010
NewSquare->makeClosed();
#endif

es=NewSquare->setColorIndex(Color);
if(es!=Acad::eOk)
{
acutPrintf(_T("\n设置对象参数出现问题,操作停止"));
NewSquare->close(); 
point1->close();//释放资源
point2->close();//释放资源
point3->close();//释放资源
point4->close();//释放资源
es=pBlockTableRecord->close();
if(es!=Acad::eOk)
{
acutPrintf(_T("\n关闭模型空间块表记录失败,错误码为:%s"),
acadErrorStatusText(es));
}
return es;

}
//
es=NewSquare->setLayer(Layer.c_str());
if(es==Acad::eKeyNotFound ||es==Acad::eDeletedEntry )//设置图层颜色时碰到图层不存在或。。
{
acutPrintf(_T("\n图层设置出现问题,操作停止"));
NewSquare->close(); 
point1->close();//释放资源
point2->close();//释放资源
point3->close();//释放资源
point4->close();//释放资源
es=pBlockTableRecord->close();
if(es!=Acad::eOk)
{
acutPrintf(_T("关闭模型空间块表记录失败,错误码为:%s\n"),
acadErrorStatusText(es));
}
return es;
}
//
if(linetype.size()!=0)//设置线型,空就不设定线型
{
AcDbObjectId lineTypeId;
if(getLinetypeIdFromString(linetype,lineTypeId))//获得线型id成功
{
if((es=NewSquare->setLinetype(lineTypeId))!=Acad::eOk)
{
acutPrintf(_T("\n线型设置出现问题,操作停止"));
NewSquare->close(); 
point1->close();//释放资源
point2->close();//释放资源
point3->close();//释放资源
point4->close();//释放资源
es=pBlockTableRecord->close();
if(es!=Acad::eOk)
{
acutPrintf(_T("\n关闭模型空间块表记录失败,错误码为:%s"),
acadErrorStatusText(es));
}
return es;
}
}
else
{
acutPrintf(_T("\n获得线型id出现问题,操作停止"));
NewSquare->close(); 
point1->close();//释放资源
point2->close();//释放资源
point3->close();//释放资源
point4->close();//释放资源
es=pBlockTableRecord->close();
if(es!=Acad::eOk)
{
acrx_abort(_T("关闭模型空间块表记录失败,错误码为:%s\n"),
acadErrorStatusText(es));
}
return es;

}

if((es=NewSquare->setLinetypeScale(1))!=Acad::eOk)
{
acutPrintf(_T("\n线型Scale设置出现问题,操作停止"));
NewSquare->close(); 
point1->close();//释放资源
point2->close();//释放资源
point3->close();//释放资源
point4->close();//释放资源
es=pBlockTableRecord->close();
if(es!=Acad::eOk)
{
acutPrintf(_T("\n关闭模型空间块表记录失败,错误码为:%s"),
acadErrorStatusText(es));
}
return es;
}

}
AcDbObjectId Id1;
es=pBlockTableRecord->appendAcDbEntity(Id1,NewSquare);
if(es!=Acad::eOk)//未能正确加入pBlockTableRecord
{
acutPrintf(_T("\n未能正确加入pBlockTableRecord"));
}
else
{ //加入成功
Id.append(Id1);

}
es=NewSquare->close();//释放资源
es=point1->close();//释放资源
es=point2->close();//释放资源
es=point3->close();//释放资源
es=point4->close();//释放资源
es=point4->close();//释放资源
}

es=pBlockTableRecord->close();
if(es!=Acad::eOk)
{
acutPrintf(_T("\n关闭模型空间块表记录失败,错误码为:%s"),
acadErrorStatusText(es));
}
return es;
}
AcDb3dPolyline* Cad_Line_Gon::build_rectang(AcGePoint3d left_bottom_point,AcGePoint3d right_top_point)
{
AcDb3dPolyline *NewSquare=NULL;
NewSquare=new AcDb3dPolyline();//建立闭合多段线对象
AcDb3dPolylineVertex *point1=new AcDb3dPolylineVertex(left_bottom_point)  ;//左下
AcDb3dPolylineVertex *point2=new AcDb3dPolylineVertex(AcGePoint3d(right_top_point.x,left_bottom_point.y,0))  ;//右下
AcDb3dPolylineVertex *point3=new AcDb3dPolylineVertex(right_top_point);//右上
AcDb3dPolylineVertex *point4=new AcDb3dPolylineVertex(AcGePoint3d(left_bottom_point.x,right_top_point.y,0))  ;//左上
NewSquare->appendVertex(point1);
NewSquare->appendVertex(point2);
NewSquare->appendVertex(point3);
NewSquare->appendVertex(point4);
if(euqal_point_2d_using_eps_for_topo(right_top_point,AcGePoint3d(left_bottom_point.x,right_top_point.y,0)))
{
delete NewSquare;
return NULL;
}
#ifdef higher_than_cad_arx_2012
NewSquare->setClosed(1); 
#endif
#ifdef cad_arx_2010
NewSquare->makeClosed();
#endif
#ifdef lower_than_cad_arx_2010
NewSquare->makeClosed();
#endif
return NewSquare;
}
AcDbRegion* Cad_Line_Gon::build_one_regions(AcDbVoidPtrArray& curveSegments)
{
int iCount=0;
AcDbVoidPtrArray regions;
if ((AcDbRegion::createFromCurves(curveSegments, regions))!= Acad::eOk)
{
acutPrintf(_T("\nbuild_one_regions创建区域对象失败"));//
iCount= regions.length();
for(int i = 0; i < iCount; i++)
{
((AcDbRegion *)regions.at(i))->close();
}
return 0;
}

AcDbRegion * pRegion=NULL;
if ((iCount = regions.length()) == 0){
acutPrintf(_T("\nbuild_one_regions创建临时区域对象为空"));
return 0;
}
if (iCount > 1)
{
// 多个 AcDbRegion , 
acutPrintf(_T("\nbuild_one_regions创建多个区域实体,取消"));
int iCount= regions.length();
for(int i = 0; i < iCount; i++)
{
((AcDbRegion *)regions.at(i))->close();
}
return 0;
}
//下面是iCount =1的情况
pRegion = (AcDbRegion *) regions.at(0);
return pRegion;
}
//函数名称:DrawSplinePolyline
//功能描述:画圆滑多段线
//依靠函数:getLinetypeIdFromString
//变量名:polylineId 变量类型:AcDbObjectId&     变量说明:得到的多段线id
//变量名:ptArr 变量类型:AcGePoint3dArray& 变量说明:输入的端点向量
//变量名:Layer 变量类型: 变量说明:多段线所在的图层
//变量名:linetype 变量类型: 变量说明:线型,输入空的值或者系统不存在的值将用实线表示
/*
//设置多段线的四个矢量
AcGePoint3dArray ptArr;
ptArr.setLogicalLength(4);
for (int i = 0; i < 4; i++) 
 {
 ptArr[i].set((double)(i/2), (double)(i%2), 0.0);
 }
AcDbObjectId  polylineId;
int ColorIndex=50;
double Width=2;
bool IsClose=0;
wstring LayerName=_T("HAHA号");
DrawSplinePolyline(polylineId, ptArr,ColorIndex,Width,IsClose,LayerName,_T("DASHED"));
*/
Acad::ErrorStatus Cad_Line_Gon::Draw_SplinePolyline(AcDbObjectId& polylineId, 
AcGePoint3dArray ptArr, int Color, double Width,bool IsClose,std::wstring Layer,std::wstring linetype)
{
Acad::ErrorStatus es=Acad::eOk; 
AcDb2dPolyline *pNewPline=NULL;
if(IsClose)
pNewPline=new AcDb2dPolyline(AcDb::k2dQuadSplinePoly,ptArr,0,Adesk::kTrue,Width,Width);
else 
pNewPline=new AcDb2dPolyline(AcDb::k2dQuadSplinePoly,ptArr,0,Adesk::kFalse,Width,Width);

if((es=pNewPline->setColorIndex(Color))!=Acad::eOk)
{
pNewPline->close();
return es;
}
if(Layer.size()!=0)
{
if(pNewPline->setLayer(Layer.c_str())==Acad::eKeyNotFound \
||pNewPline->setLayer(Layer.c_str())==Acad::eDeletedEntry )
{
creat_new_layer(Layer.c_str(),NULL);//建立新图层
if((es=pNewPline->setLayer(Layer.c_str()))!=Acad::eOk)
{
pNewPline->close();
return es;
}
}
}
if(linetype.size()!=0)
{
AcDbObjectId lineTypeId;
if(getLinetypeIdFromString(linetype,lineTypeId))
{
if((es=pNewPline->setLinetype(lineTypeId))!=Acad::eOk)
{
pNewPline->close();
return es;
}
if((es=pNewPline->setLinetypeScale(1))!=Acad::eOk)
{
pNewPline->close();
return es;
}
}
}
if(!pNewPline->isLinetypeGenerationOn())
{
if((es=pNewPline->setLinetypeGenerationOn())!=Acad::eOk)
{
pNewPline->close();
return es;
}
}
////////////////////////////////////////////////////////////
AcDbBlockTableRecord *pBlockTableRecord=NULL;
es=Cad_BlockTableRecord<AcDbBlockTableRecord,std::vector<AcDbBlockTableRecord> >::OpenSpace(pBlockTableRecord,acdbHostApplicationServices()->workingDatabase()); 
if(es!=Acad::eOk)
{
acutPrintf(_T("\n获得pBlockTableRecord操作未能完成"));
pNewPline->close();
return es;
}
es=pBlockTableRecord->appendAcDbEntity(polylineId,pNewPline);
if(es!=Acad::eOk)//未能正确加入pBlockTableRecord
{
pNewPline->close();
Acad::ErrorStatus es2=pBlockTableRecord->close();
if (es2!=Acad::eOk)
{
acutPrintf(_T("\n关闭模型空间块表记录失败,错误码为:%s"),
acadErrorStatusText(es2));
}
return es;
}
else
{
es=pNewPline->close();
if(es!=Acad::eOk)
{
acutPrintf(_T("\n应用程序X关闭实体失败,错误码为:%d"),
acadErrorStatusText(es));
}

es=pBlockTableRecord->close();
if(es!=Acad::eOk)
{
acutPrintf(_T("\n关闭模型空间块表记录失败,错误码为:%s"),
acadErrorStatusText(es));
}

}
return es;

}
//
//函数名称:CreateHatch
//函数类型:AcDbObjectId
//功能描述:在封闭区间填充图案(功能简单,输入条件严格)
//变量名:dbOId 变量类型:AcDbObjectId 变量说明:闭合多段线
//变量名:pDbDatab 变量类型:AcDbDatabase *   变量说明:指定填充图案保存的数据库,默认为当前
/*例子:
AcGePoint3dArray ptXiejiaoArr1;
ptXiejiaoArr1.setLogicalLength(3);
ptXiejiaoArr1[0].set(0,0,0.0);
ptXiejiaoArr1[1].set(10,0,0.0);
ptXiejiaoArr1[2].set(10,10,0.0);

AcDb2dPolyline *pXieqiePline1;

AcDbObjectId XieqieId1;

pXieqiePline1=new AcDb2dPolyline(AcDb::k2dSimplePoly,ptXiejiaoArr1,0.0,Adesk::kTrue);


XieqieId1=AddToModelSpace(pXieqiePline1);
pXieqiePline1->close();
CreateHatch(XieqieId1, _T("0"), _T("SOLID"), 256, 0.0, 1.0,  acdbHostApplicationServices()->workingDatabase());
*/
AcDbObjectId Cad_Line_Gon::Draw_Hatch(
AcDbObjectId dbOId,
wchar_t cLayer[], 
wchar_t cPattern[], 
int nColor, 
double dAngle, 
double dScale, 
AcDbDatabase * pDbDatab)
{
AcCmColor CmC;
AcDbObjectId DbOId;
AcDbObjectIdArray DbOIdA(0, 2);
AcDbBlockTable * pDbBT=NULL;
AcDbBlockTableRecord * pDbBTR=NULL;
AcGeVector3d normal(0.0, 0.0, 1.0);

DbOIdA.append(dbOId);

AcDbHatch* pDbHat =new AcDbHatch();
pDbHat->setDatabaseDefaults();
pDbHat->setAssociative(Adesk::kTrue); // BUG: doesn't do squat! have to set the reactor yourself to get associativity!
pDbHat->appendLoop(AcDbHatch::kExternal, DbOIdA);
pDbHat->setPatternScale(dScale);
pDbHat->setPatternAngle(dAngle);
pDbHat->setPattern(AcDbHatch::kPreDefined, cPattern);
pDbHat->setNormal(normal);
pDbHat->evaluateHatch(); // crucial call or nothing gets displayed!
pDbHat->setLayer(cLayer);
CmC.setColorIndex(nColor);
((AcDbEntity *)pDbHat)->setColor(CmC);
pDbDatab->getSymbolTable(pDbBT, AcDb::kForRead);
pDbBT->getAt(ACDB_MODEL_SPACE, pDbBTR, AcDb::kForWrite);
pDbBTR->appendAcDbEntity(DbOId, pDbHat);
pDbBT->close();
pDbBTR->close();
pDbHat->close();
return DbOId;
}
Acad::ErrorStatus Cad_Line_Gon::Draw_Hatch(
AcDbObjectIdArray &FanHuiId,
AcDbObjectIdArray &dbOId,
std::vector < std::wstring > &cLayer, 
std::vector < std::wstring >  &cPattern, 
std::vector < int > &nColor, 
std::vector < double > &dAngle, 
std::vector < double > &dScale, 
AcDbDatabase * pDbDatab)
{
FanHuiId.removeAll();
Acad::ErrorStatus es=Acad::eOk; 
int i=dbOId.length();
if (!i)
{
acutPrintf(_T("\n没有输入填充边界"));
return Acad::eOk; 
}
std::vector < std::wstring > ::iterator cLayer_iterator=cLayer.begin();
std::vector < std::wstring > ::iterator cPattern_iterator=cPattern.begin();
std::vector < int > ::iterator nColor_iterator=nColor.begin(); 
std::vector < double > ::iterator dAngle_iterator=dAngle.begin(); 
std::vector < double > ::iterator dScale_iterator=dScale.begin(); 

AcDbBlockTableRecord * pDbBTR=NULL;
es=Cad_BlockTableRecord<AcDbBlockTableRecord,std::vector<AcDbBlockTableRecord> >::OpenSpace(pDbBTR,pDbDatab);

for ( int k=0 ;k<i;k++,cLayer_iterator++,cPattern_iterator++,nColor_iterator++,dAngle_iterator++, dScale_iterator++)
{
AcCmColor CmC;
AcDbObjectId DbOId;
AcDbObjectIdArray DbOIdA(0, 2);

AcGeVector3d normal(0.0, 0.0, 1.0);

DbOIdA.append(dbOId.at(k));

AcDbHatch* pDbHat =new AcDbHatch();

pDbHat->setDatabaseDefaults();
pDbHat->setElevation(0.0);
es=pDbHat->setNormal(normal);
// Set non associative hatch
// Set hatch pattern to SolidFill type
es=pDbHat->setPattern(AcDbHatch::kPreDefined,cPattern_iterator->c_str() );

es=pDbHat->setAssociative(Adesk::kTrue); // BUG: doesn't do squat! have to set the reactor yourself to get associativity!

es=pDbHat->appendLoop(AcDbHatch::kExternal, DbOIdA);

es=pDbHat->setPatternScale(*dScale_iterator);

es=pDbHat->setPatternAngle(*dAngle_iterator);

es=pDbHat->evaluateHatch(); // crucial call or nothing gets displayed!

es=pDbHat->setLayer(cLayer_iterator->c_str());

es=CmC.setColorIndex(*nColor_iterator);

((AcDbEntity *)pDbHat)->setColor(CmC);

es=pDbBTR->appendAcDbEntity(DbOId, pDbHat);//加入数据库
FanHuiId.append(DbOId);
pDbHat->close();
}

pDbBTR->close();
if(es!=Acad::eOk)
{
acutPrintf(_T("\n填充时出现问题,请注意"));
}
return es;
}


//************************************************************************
//函数名称:GetMirrorEntity
//功能描述:对指定单个di对象镜象||简单克隆
//变量名:Id 变量说明:需要进行镜象的id
//变量名:ptMirAxisStart   ptMirAxisEnd   变量说明:镜象线
//************************************************************************/
Acad::ErrorStatus Cad_Line_Gon::Draw_MirrorEntity( AcDbObjectId &Fanhui_Id,AcDbObjectId &Shuru_Id,
AcGePoint3d ptMirAxisStart,
AcGePoint3d ptMirAxisEnd,AcDbDatabase *Database)
{
Acad::ErrorStatus es=Acad::eOk; 
//建立变换矩阵
AcGeMatrix3d xform1;
xform1.setToMirroring(AcGeLine3d(ptMirAxisStart,ptMirAxisEnd));
AcDbObject * pEnt=NULL;
es=acdbOpenAcDbObject(pEnt,Shuru_Id,AcDb::kForRead);//打开id对象,取得资源
if(es==Acad::eOk)//是否成功打开
{
AcDbEntity* pEnt0=(AcDbEntity*)pEnt->clone();//克隆对象
es=pEnt->close();//释放原资源
es=pEnt0->transformBy(xform1);//被克隆的对象变换
AcDbBlockTableRecord *Record;
es=Cad_BlockTableRecord<AcDbBlockTableRecord,std::vector<AcDbBlockTableRecord> >::OpenSpace(Record,Database);//获得AcDbBlockTableRecord资源
es=Record->appendAcDbEntity(Fanhui_Id,pEnt0);
es=Record->close();//释放资源
es=pEnt0->close();//释放资源
}
if(es!=Acad::eOk) 
acutPrintf(_T("\n单对象实体镜象存在问题"));
return es;
}

//************************************************************************
//函数名称:GetMirrorEntity
//函数类型:AcDbObjectId
//功能描述:对指定多个di对象镜象||简单克隆
/*AcDbObjectIdArray Id;
std::vector < AcGePoint3d > y;
std::vector < double > x;

for (int i=0;i<10;i++)
{ AcGePoint3d p(0,10*i,0);
y.push_back(p) ;

}
std::vector < std::wstring > cLayer; 
std::vector < std::wstring >  cPattern; 
std::vector < int > nColor; 
std::vector < double > dAngle; 
std::vector < double > dScale; 
for (int i=0;i<10-1;i++)
{
x.push_back(10);
cLayer.push_back(std::wstring(_T("0"))); 
cPattern.push_back(std::wstring(_T("SOLID")));
nColor.push_back(2); 
dAngle.push_back(1); 
dScale.push_back(1); 
}
Cad_Line_Gon a1;

a1.DrawSquare(Id,x,y,3,1);
AcDbObjectIdArray FanHuiId;
AcDbObjectId Idfanhui;
a1.CreateHatch(FanHuiId,Id,cLayer, cPattern, nColor, dAngle, dScale);
a1.GetMirrorEntity(Idfanhui,FanHuiId.at(1),AcGePoint3d(100,0,0),AcGePoint3d(100,100,0));
AcDbObjectIdArray FanHuiId1;
a1.GetMirrorEntity(FanHuiId1,FanHuiId,AcGePoint3d(100,0,0),AcGePoint3d(100,100,0));
*//************************************************************************/
Acad::ErrorStatus Cad_Line_Gon::Draw_MirrorEntity( AcDbObjectIdArray &Fanhui_Id,AcDbObjectIdArray &Shuru_Id,
AcGePoint3d ptMirAxisStart,
AcGePoint3d ptMirAxisEnd,AcDbDatabase *Database)
{
Acad::ErrorStatus es=Acad::eOk; 
Fanhui_Id.removeAll();
//建立变换矩阵
AcGeMatrix3d xform1;
xform1.setToMirroring(AcGeLine3d(ptMirAxisStart,ptMirAxisEnd));
//获得记录表资源
AcDbObject * pEnt=NULL;
AcDbBlockTableRecord *Record=NULL;
es=Cad_BlockTableRecord<AcDbBlockTableRecord,std::vector<AcDbBlockTableRecord> >::OpenSpace(Record,Database);//获得AcDbBlockTableRecord资源
for (int i=0;i<Shuru_Id.length();i++)
{
es=acdbOpenAcDbObject(pEnt,Shuru_Id.at(i),AcDb::kForRead);//打开id对象,取得资源
if(es==Acad::eOk)//是否成功打开
{
AcDbEntity* pEnt0=(AcDbEntity*)pEnt->clone();//克隆对象
es=pEnt->close();//释放原资源
es=pEnt0->transformBy(xform1);//被克隆的对象变换

AcDbObjectId id;
es=Record->appendAcDbEntity(id,pEnt0);
Fanhui_Id.append(id);
es= pEnt0->close();//释放资源
}
}
es=Record->close();//释放Record资源
if(es!=Acad::eOk) 
acutPrintf(_T("\n多对象实体镜象存在问题"));
return es;
}
}


路过

雷人

握手

鲜花

鸡蛋

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

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

GMT+8, 2024-5-11 23:56 , Processed in 0.123854 second(s), 15 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部