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

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

日志

(源码)一些通用函数,尽量含有错误提示

已有 785 次阅读2013-11-19 15:11 |系统分类:应用| 通用arx函数

提供给想学习arx和刚入门的同学一些参考例子。
基本内容:(看函数名,起的不怎么好;如果使用请检查;头文件和实现文件内容在后;发现错误请跟帖提示一下)
 void cad_zoom();
void cad_zoom_win();
void cad_zoom(AcGePoint2d CenterPt,double Height,double Width);

 void SendCmdToAcad(std::wstring &Cmd);

void change_cad_title(std::wstring title);
std::wstring get_active_database_tile_name(void);
std::wstring get_database_tile_name(AcDbDatabase* input_database);
AcApDocument*  get_database_AcApDocument(AcDbDatabase* input_database);
std::wstring get_cad_file_path_and_name(void);

std::wstring printf_sys_time(std::wstring text);
void throw_error(Acad::ErrorStatus err);//抛出异常//没用过
       
bool is_showed(AcDbEntity *pEnt);//

//添加cad的支持路径
bool AddSupportPath(std::vector<std::wstring> &arPath);

//遍历指定对象,获得该对象所有元素的拷贝//m_为变换矩阵,输入空的对象
template<class T>//不清空store原有内容,累加
bool recursive_procedure_for_copy(AcDbBlockReference *Ent,std::vector<T *>& store,AcGeMatrix3d m_) ;

//如果存在该层,可以对AcDbDatabase* pDb输入NULL
bool has_the_same_lay(std::wstring inputLyName,AcDbDatabase* pDb);
bool creat_new_layer(std::wstring inputLyName,AcDbDatabase* pDb);

//在cad图上以可读或可写的方式获得指定AcDbDatabase资源的AcDbBlockTableRecord
AcDbObjectId get_AcDbBlockTableRecord(AcDbBlockTableRecord *&pSpaceRecord ,std::wstring block_name=ACDB_MODEL_SPACE,AcDb::OpenMode mode=AcDb::kForWrite, AcDbDatabase* pDb= acdbHostApplicationServices()->workingDatabase());

//加入模型空间
template<class T>
AcDbObjectId AddToModelSpace(T *pEnt);
template<class containor>
AcDbObjectIdArray  AddToModelSpace(containor &pEnt);
template<class containor>
std::vector<AcDbObjectId>  AddToModelSpace_return_vector(containor &pEnt);
//和块相关
bool has_the_same_block(std::wstring BlockName, AcDbDatabase *i_pDb= acdbHostApplicationServices()->workingDatabase());
//
std::wstring get_a_different_block_name_if_input_name_had_exited(std::wstring BlockName,AcDbDatabase *i_pDb= acdbHostApplicationServices()->workingDatabase());
       
//功能:通过id打开资源
template<class T>
Acad::ErrorStatus  change_AcDbObjectId_to_AcDbEntity(std::vector< T* > &fanhui_object_pt_vector,const AcDbObjectIdArray &object_id,bool add=0,bool write=1);
template<class T>
Acad::ErrorStatus  change_AcDbObjectId_to_AcDbEntity(std::set< T* > &fanhui_object_pt_vector,const AcDbObjectIdArray &object_id,bool add=0,bool write=1);
template<class T>
Acad::ErrorStatus  change_AcDbObjectId_to_AcDbEntity(T*  &fanhui_object, AcDbObjectId &object_id,bool write=1);
 
//功能:通过资源,获取id,但只限定已经加入模型空间的资源,原来资源将会释放,返回过程中不能转换的个数
template<class T>
int change_AcDbEntity_to_AcDbObjectId(AcDbObjectIdArray &object_id, std::vector< T* > &fanhui_object_pt_vector);
template<class T>
int change_AcDbEntity_to_AcDbObjectId(AcDbObjectIdArray &object_id, std::set< T* > &fanhui_object_pt_vector);

 //将cad资源设置为可读形式
template<class T>
inline Acad::ErrorStatus  make_entity_ForRead(T *import_enty);
template<class T>
Acad::ErrorStatus  make_entity_ForRead(std::set< T * > & import_enty);
template<class T>
Acad::ErrorStatus  make_entity_ForRead(std::vector< T * > & import_enty);
Acad::ErrorStatus  make_entity_ForRead(AcDbVoidPtrArray& curveSegments);
template<class T>
Acad::ErrorStatus  make_entity_ForWrite(std::vector< T * > & import_enty);
//将cad资源设置为可写形式
template<class T>
inline Acad::ErrorStatus  make_entity_ForWrite(T *import_enty);
template<class T>
Acad::ErrorStatus  make_entity_ForWrite(std::set< T * > & import_enty);
Acad::ErrorStatus  make_entity_ForWrite(AcDbVoidPtrArray& curveSegments);

//在cad图上获得一个指定的属于T类型的资源(cad命令行中有提示)
//更改cad中某一类的颜色
template<class T>
void change_entity_colour(std::vector<T *>&input,Adesk::UInt16 color);
template<class T>
void change_entity_colour(std::set<T *>&input,Adesk::UInt16 color);

//克隆
template<class T>
Acad::ErrorStatus  clone_entity( std::vector< T * > & export_enty,const std::vector< T * > & import_enty);
template<class T>
Acad::ErrorStatus  clone_entity( std::set< T * > & export_enty,const std::vector< T * > & import_enty);
template<class T>
Acad::ErrorStatus  clone_entity( std::set< T * > & export_enty,const std::set< T * > & import_enty);
template<class T>
Acad::ErrorStatus  clone_entity( std::vector< T * > & export_enty,const std::vector< AcDbObjectId > & import);
Acad::ErrorStatus  clone_entity( AcDbEntity *&export_enty,const AcDbObjectId import);
//功能:根据id  copy指定类型的对象组,以可写的形式获得资源
template<class T>
Acad::ErrorStatus  clone_entity( std::vector< T * > & export_enty,const AcDbObjectIdArray & import);
//删除
template<class T>
void erase_entity(std::vector<T*> &shuru);
template<class T>
void erase_entity(std::set<T*> &shuru);

//释放资源
template<class T>
void close_entity(std::vector<T*> &shuru);
template<class T>
void close_entity(std::set<T*> &shuru);
void close_entity(AcGeVoidPointerArray &shuru);

\\头文件
#pragma once
#ifndef _function_for_cad_1_H 
#define _function_for_cad_1_H
#include"../../../cad2012+arx/函数库/geometry_lib/geometry_lib.h"//AcDbDatabase 等;
#include"../../../cad2012+arx/转换/format_change/format_change.h"
#include"../../../cad2012+arx/转换/AcDbCurveToAcGeCurve.h"
#include"../../../cad2012+arx\\cad系统操作\\Select\\Select.h" 
//#include <windows.h> 
//#include<assert.h>
using namespace xtcow;
namespace xtcow
{
void cad_zoom();
void cad_zoom_win();
void cad_zoom(AcGePoint2d CenterPt,double Height,double Width);

void SendCmdToAcad(std::wstring &Cmd);

void change_cad_title(std::wstring title);
std::wstring get_active_database_tile_name(void);
std::wstring get_database_tile_name(AcDbDatabase* input_database);
AcApDocument*  get_database_AcApDocument(AcDbDatabase* input_database);
std::wstring get_cad_file_path_and_name(void);

std::wstring printf_sys_time(std::wstring text);
void throw_error(Acad::ErrorStatus err);//抛出异常//没用过

bool is_showed(AcDbEntity *pEnt);//

//添加cad的支持路径
bool AddSupportPath(std::vector<std::wstring> &arPath);

//遍历指定AcDbBlockReference对象,获得该对象所有可见元素的拷贝
template<class T>
bool recursive_procedure_for_copy(AcDbBlockReference *Ent,std::vector<T *>& store,AcGeMatrix3d m_) ;

//如果存在该层,可以对AcDbDatabase* pDb输入NULL
bool has_the_same_lay(std::wstring inputLyName,AcDbDatabase* pDb);
bool creat_new_layer(std::wstring inputLyName,AcDbDatabase* pDb);

//在cad图上以可读或可写的方式获得指定AcDbDatabase资源的AcDbBlockTableRecord
AcDbObjectId get_AcDbBlockTableRecord(AcDbBlockTableRecord *&pSpaceRecord ,std::wstring block_name=ACDB_MODEL_SPACE,AcDb::OpenMode mode=AcDb::kForWrite, AcDbDatabase* pDb= acdbHostApplicationServices()->workingDatabase());

//加入模型空间
template<class T>
AcDbObjectId AddToModelSpace(T *pEnt);
template<class containor>
AcDbObjectIdArray  AddToModelSpace(containor &pEnt);
template<class containor>
std::vector<AcDbObjectId>  AddToModelSpace_return_vector(containor &pEnt);

//和块相关
bool has_the_same_block(std::wstring BlockName, AcDbDatabase *i_pDb= acdbHostApplicationServices()->workingDatabase());
std::wstring get_a_different_block_name_if_input_name_had_exited(std::wstring BlockName,AcDbDatabase *i_pDb= acdbHostApplicationServices()->workingDatabase());

//功能:通过id打开资源
template<class T>
Acad::ErrorStatus  change_AcDbObjectId_to_AcDbEntity(std::vector< T* > &fanhui_object_pt_vector,const AcDbObjectIdArray &object_id,bool add=0,bool write=1);
template<class T>
Acad::ErrorStatus  change_AcDbObjectId_to_AcDbEntity(std::set< T* > &fanhui_object_pt_vector,const AcDbObjectIdArray &object_id,bool add=0,bool write=1);
template<class T>
Acad::ErrorStatus  change_AcDbObjectId_to_AcDbEntity(T*  &fanhui_object, AcDbObjectId &object_id,bool write=1);

//功能:通过资源,获取id,但只限定已经加入模型空间的资源,原来资源将会释放,返回过程中不能转换的个数
template<class T>
int change_AcDbEntity_to_AcDbObjectId(AcDbObjectIdArray &object_id, std::vector< T* > &fanhui_object_pt_vector);
template<class T>
int change_AcDbEntity_to_AcDbObjectId(AcDbObjectIdArray &object_id, std::set< T* > &fanhui_object_pt_vector);

//将cad资源设置为可读形式
template<class T>
inline Acad::ErrorStatus  make_entity_ForRead(T *import_enty);
template<class T>
Acad::ErrorStatus  make_entity_ForRead(std::set< T * > & import_enty);
template<class T>
Acad::ErrorStatus  make_entity_ForRead(std::vector< T * > & import_enty);
Acad::ErrorStatus  make_entity_ForRead(AcDbVoidPtrArray& curveSegments);
template<class T>
Acad::ErrorStatus  make_entity_ForWrite(std::vector< T * > & import_enty);

//将cad资源设置为可写形式
template<class T>
inline Acad::ErrorStatus  make_entity_ForWrite(T *import_enty);
template<class T>
Acad::ErrorStatus  make_entity_ForWrite(std::set< T * > & import_enty);
Acad::ErrorStatus  make_entity_ForWrite(AcDbVoidPtrArray& curveSegments);

//在cad图上获得一个指定的属于T类型的资源(cad命令行中有提示)
//更改cad中某一类的颜色
template<class T>
void change_entity_colour(std::vector<T *>&input,Adesk::UInt16 color);
template<class T>
void change_entity_colour(std::set<T *>&input,Adesk::UInt16 color);

//克隆
template<class T>
Acad::ErrorStatus  clone_entity( std::vector< T * > & export_enty,const std::vector< T * > & import_enty);
template<class T>
Acad::ErrorStatus  clone_entity( std::set< T * > & export_enty,const std::vector< T * > & import_enty);
template<class T>
Acad::ErrorStatus  clone_entity( std::set< T * > & export_enty,const std::set< T * > & import_enty);
template<class T>
Acad::ErrorStatus  clone_entity( std::vector< T * > & export_enty,const std::vector< AcDbObjectId > & import);
Acad::ErrorStatus  clone_entity( AcDbEntity *&export_enty,const AcDbObjectId import);
//功能:根据id  copy指定类型的对象组,以可写的形式获得资源
template<class T>
Acad::ErrorStatus  clone_entity( std::vector< T * > & export_enty,const AcDbObjectIdArray & import);

//删除
template<class T>
void erase_entity(std::vector<T*> &shuru);
template<class T>
void erase_entity(std::set<T*> &shuru);

//释放资源
template<class T>
void close_entity(std::vector<T*> &shuru);
template<class T>
void close_entity(std::set<T*> &shuru);
void close_entity(AcGeVoidPointerArray &shuru);


/*************************************************************************************************************************************/
//定义
//遍历指定AcDbBlockReference对象,获得该对象所有可见元素的拷贝////不清空store原有内容,累加//递归调用,使用时m_输入为空
template<class T>
bool recursive_procedure_for_copy(AcDbBlockReference *Ent,std::vector<T *>& store,AcGeMatrix3d m_) 
{
Acad::ErrorStatus es=Acad::eOk;
if(Ent==0) 
return 0;
AcDbObjectId id=Ent->blockTableRecord();
//打开这个外部参照图块物体
AcDbObject *database=0;
es=acdbOpenObject(database, id, AcDb::kForRead);
if (es!= Acad::eOk)
{
return 0;
}
AcDbBlockTableRecord *BlockTable=0;
BlockTable = AcDbBlockTableRecord::cast(database);
//
AcDbBlockTableRecordIterator* pBlockIt=0;
es=BlockTable->newIterator(pBlockIt);
if(Acad::eOk != es)//通过table获得set
{
es =BlockTable->close();
return 0;
}
int count=0;//不是用来计数的,只是表征存在与否
AcGeMatrix3d m= Ent->blockTransform();
m_.postMultBy(m);
for(pBlockIt->start();!pBlockIt->done();pBlockIt->step())//遍历BlockTable
{
AcDbEntity* pEnt1=NULL;
es = pBlockIt->getEntity(pEnt1, AcDb::kForRead);
if (Acad::eOk == es)
{
if(is_showed(pEnt1))//可见的
{
   if (!pEnt1->isKindOf(AcDbBlockReference::desc()))//不是参照
{
if(pEnt1->isKindOf(T::desc()))
{
//
T *k=T::cast((pEnt1)->clone());
k->transformBy(m_);
store.push_back(k);
es =pEnt1->close();
count++;
}
else
{
es =pEnt1->close();
}
}
else
{//参照
count+=recursive_procedure_for_copy(AcDbBlockReference::cast(pEnt1),store,m_);
pEnt1->close();
}
}
else
{
pEnt1->close();
}

}

}
delete pBlockIt;
BlockTable->close();
if(count!=0)
return 1;
else
return 0;
}
//遍历指定对象,获得该对象所有元素的拷贝
template<class T>//不清空store原有内容,累加
bool recursive_procedure_for_copy(AcDbBlockReference *Ent,std::set<T *>& store,AcGeMatrix3d m_) 
{
Acad::ErrorStatus es=Acad::eOk;
if(Ent==0) 
return 0;
AcDbObjectId id=Ent->blockTableRecord();
//打开这个外部参照图块物体
AcDbObject *database=0;
es=acdbOpenObject(database, id, AcDb::kForRead);
if (es!= Acad::eOk)
{
return 0;
}
AcDbBlockTableRecord *BlockTable=0;
BlockTable = AcDbBlockTableRecord::cast(database);
//
AcDbBlockTableRecordIterator* pBlockIt=0;
es=BlockTable->newIterator(pBlockIt);
if(Acad::eOk != es)//通过table获得set
{
es =BlockTable->close();
return 0;
}
int count=0;//不是用来计数的,只是表征存在与否
AcGeMatrix3d m= Ent->blockTransform();
m_.postMultBy(m);
for(pBlockIt->start();!pBlockIt->done();pBlockIt->step())//遍历BlockTable
{
AcDbEntity* pEnt1=0;
es = pBlockIt->getEntity(pEnt1, AcDb::kForRead);
if (Acad::eOk == es)
{
if(is_showed(pEnt1))//可见的
{
   if (!pEnt1->isKindOf(AcDbBlockReference::desc()))//不是参照
{
if(pEnt1->isKindOf(T::desc()))
{
//
T *k=T::cast((pEnt1)->clone());
k->transformBy(m_);
store.insert(k);
es =pEnt1->close();
count++;
}
else
{
es =pEnt1->close();
}
}
else
{//参照
count+=recursive_procedure_for_copy(AcDbBlockReference::cast(pEnt1),store,m_);
pEnt1->close();
}
}
else
{
pEnt1->close();
}

}

}
delete pBlockIt;
BlockTable->close();
if(count!=0)
return 1;
else
return 0;
}
template<class T>
Acad::ErrorStatus  make_entity_ForRead(std::set< T * > & import_enty)
{
Acad::ErrorStatus es=Acad::eOk;
std::set< T * > ::iterator begin=import_enty.begin();
for (;begin!=import_enty.end();begin++)//将输入的线对象改成只读形式来打开
{
if ((*begin)==0)
{
continue;
}
if ((*begin)->isWriteEnabled())
{
AcDbObjectId kk=(*begin)->id();
if (kk==0)
{
continue;
}
(*begin)->close();
//
T * linshi=0;
es=acdbOpenObject(linshi,kk,AcDb::kForRead);
if(linshi!=0) 
{
(*begin)=linshi;
}
else
{
(*begin)=linshi;
acutPrintf(_T("\nmake_entity_ForRead出现一个问题"));
}
}
}
return es;
}
template<class T>
Acad::ErrorStatus  make_entity_ForRead(std::vector< T * > & import_enty)
{
Acad::ErrorStatus es=Acad::eOk;
std::vector< T * > ::iterator begin=import_enty.begin();
for (;begin!=import_enty.end();begin++)//将输入的线对象改成只读形式来打开
{
if ((*begin)==0)
{
continue;
}
if ((*begin)->isWriteEnabled())
{
AcDbObjectId kk=(*begin)->id();
if (kk==0)
{
continue;
}
(*begin)->close();
T * linshi=0;
es=acdbOpenObject(linshi,kk,AcDb::kForRead);
if(linshi!=0) 
{
(*begin)=linshi;
}
else
{
(*begin)=linshi;
acutPrintf(_T("\nmake_entity_ForRead出现一个问题"));
}

}
}
return es;
}
template<class T>
Acad::ErrorStatus  make_entity_ForWrite(std::set< T * > & import_enty)
{
Acad::ErrorStatus es=Acad::eOk;
std::set< T * > ::iterator begin=import_enty.begin();
for (;begin!=import_enty.end();begin++)//将输入的线对象改成读写的形式来打开
{
if ((*begin)==0)
{
continue;
}
if ((*begin)->isReadEnabled())
{
AcDbObjectId kk=(*begin)->id();
if (kk==0)
{
continue;
}
(*begin)->close();
T * linshi=0;
es=acdbOpenObject(linshi,kk,AcDb::kForWrite);
if(linshi!=0) 
{
(*begin)=linshi;
}
else
{
(*begin)=linshi;
acutPrintf(_T("\nmake_entity_ForWrite出现一个问题"));
}
}
}
return es;
}
template<class T>
Acad::ErrorStatus  make_entity_ForWrite(std::vector< T * > & import_enty)
{
Acad::ErrorStatus es=Acad::eOk;
std::vector< T * > ::iterator begin=import_enty.begin();
for (;begin!=import_enty.end();begin++)//将输入的线对象改成读写的形式来打开
{
if ((*begin)==0)
{
continue;
}
if ((*begin)->isReadEnabled())
{
AcDbObjectId kk=(*begin)->id();
if (kk==0)
{
continue;
}
(*begin)->close();
T * linshi=0;
es=acdbOpenObject(linshi,kk,AcDb::kForWrite);
if(linshi!=0) 
{
(*begin)=linshi;
}
else
{
(*begin)=linshi;
acutPrintf(_T("\nmake_entity_ForWrite出现一个问题"));
}
}
}
return es;
}
//提供给XTCOW_Cad_point(将Tree_node,存进container容器)
template<class T>
inline Acad::ErrorStatus  make_entity_ForRead(T *import_enty)
{
Acad::ErrorStatus es=Acad::eOk;
if (import_enty->isWriteEnabled())
{
AcDbObjectId kk=import_enty->id();
if (kk==0)
{
return es;
}
import_enty->close();
import_enty=NULL;
es=acdbOpenObject(import_enty,kk,AcDb::kForRead);
}
return es;
}
template<class T>
Acad::ErrorStatus  make_entity_ForWrite(T *import_enty)
{
Acad::ErrorStatus es=Acad::eOk;
if (import_enty->isReadEnabled())
{
AcDbObjectId kk=import_enty->id();
if (kk==0)
{
return es;
}
import_enty->close();
import_enty=NULL;
es=acdbOpenObject(import_enty,kk,AcDb::kForWrite);
}
return es;
}

template<class T>
void change_entity_colour(std::vector<T *>&input,Adesk::UInt16 color)
{
std::vector<T *>::iterator begin=input.begin();
for (;begin!=input.end();begin++)
{
if (((*begin)!=0)&&AcDbEntity::cast((*begin)))
{
((AcDbEntity*)(*begin))->setColorIndex(color);
}
}
}
template<class T>
void change_entity_colour(std::set<T *>&input,Adesk::UInt16 color)
{
std::set<T *>::iterator begin=input.begin();
for (;begin!=input.end();begin++)
{
if (((*begin)!=0)&&AcDbEntity::cast((*begin)))
{
((AcDbEntity*)(*begin))->setColorIndex(color);
}
}
}
//刚开始时就是从网上下了这个开始看的
//函数名称:AddToModelSpace
//返回值:AcDbObjectIdArray 
//功能描述:增加实体组到当前模空间中
template<class containor>
AcDbObjectIdArray  AddToModelSpace(containor &pEnt)
{
Acad::ErrorStatus es=Acad::eOk;
AcDbBlockTable              *BlockTable=0;
AcDbBlockTableRecord        *BlockTableRecord=0;
AcDbObjectIdArray Id;
//这里用读打开也是可以的,因为我这样用实际没出问题
if(acdbHostApplicationServices()->workingDatabase()->getBlockTable(BlockTable, AcDb::kForRead)!=Acad::eOk)
return 0;
if(BlockTable->getAt(ACDB_MODEL_SPACE, BlockTableRecord, AcDb::kForWrite)!=Acad::eOk)
{
BlockTable->close();
return 0;
}
containor::iterator pEnt1=pEnt.begin();
for (;pEnt1!=pEnt.end();pEnt1++)
{
AcDbObjectId     entId;
if( BlockTableRecord->appendAcDbEntity(entId,*pEnt1)!=Acad::eOk)
{
BlockTable->close();
BlockTableRecord->close();
 
return 0;
}
Id.append(entId);
}
BlockTable->close();
BlockTableRecord->close();
 
return Id;
}
//函数名称:AddToModelSpace
//返回值:AcDbObjectIdArray 
//功能描述:增加实体组到当前模空间中
template<class containor>
std::vector<AcDbObjectId>  AddToModelSpace_return_vector(containor &pEnt)
{
Acad::ErrorStatus es;
AcDbBlockTable                  *BlockTable=NULL;
AcDbBlockTableRecord            *BlockTableRecord=NULL;
std::vector<AcDbObjectId> Id;
//这里用读打开也是可以的,因为我这样用实际没出问题
if(acdbHostApplicationServices()->workingDatabase()->getBlockTable(BlockTable, AcDb::kForRead)!=Acad::eOk)
return Id;

if(BlockTable->getAt(ACDB_MODEL_SPACE, BlockTableRecord, AcDb::kForWrite)!=Acad::eOk)
{
BlockTable->close();

return Id;
}
containor::iterator pEnt1=pEnt.begin();
for (;pEnt1!=pEnt.end();pEnt1++)
{
AcDbObjectId     entId;
if( BlockTableRecord->appendAcDbEntity(entId,*pEnt1)!=Acad::eOk)
{
BlockTable->close();
BlockTableRecord->close();

return Id;
}
Id.push_back(entId);
}
BlockTable->close();
BlockTableRecord->close();

return Id;
}
template<class T>
AcDbObjectId AddToModelSpace(T *pEnt)
{
Acad::ErrorStatus es=Acad::eOk;
AcDbObjectId Id;
if (pEnt==0)
{
return 0;
}
AcDbBlockTable                  *BlockTable=0;
AcDbBlockTableRecord          *BlockTableRecord=0;
if(acdbHostApplicationServices()->workingDatabase()->getBlockTable(BlockTable, AcDb::kForRead)!=Acad::eOk)
return 0;
if(BlockTable->getAt(ACDB_MODEL_SPACE, BlockTableRecord, AcDb::kForWrite)!=Acad::eOk)
{
BlockTable->close();
return 0;
}
if (BlockTableRecord->appendAcDbEntity(Id,pEnt)!=Acad::eOk)
{
BlockTableRecord->close();
BlockTable->close();
return 0;
}
BlockTableRecord->close();
BlockTable->close();
return Id;
}
template<class T>
void erase_entity(std::set<T*> &shuru)
{
if (shuru.size()==0)
{
return;
}
std::set<T*> ::iterator pt=shuru.begin();
//集中释放资源(不是曲线类的对象)
for (;pt!=shuru.end();pt++)
{
if ((*pt)!=0)
{
(*pt)->erase();
}
}
}
template<class T>
void erase_entity(std::vector<T*> &shuru)
{
if (shuru.size()==0)
{
return;
}
std::vector<T*> ::iterator pt=shuru.begin();
//集中释放资源(不是曲线类的对象)
for (;pt!=shuru.end();pt++)
{
if ((*pt)!=0)
{
(*pt)->erase();
}
}
}
template<class T>
void close_entity(std::vector<T*> &shuru)
{
if (shuru.size()==0)
{
return;
}
std::vector<T*> ::iterator pt=shuru.begin();
//集中释放资源(不是曲线类的对象)
for (;pt!=shuru.end();pt++)
{
if ((*pt)!=0)
{
(*pt)->close();
}
}
}
template<class T>
void close_entity(std::set<T*> &shuru)
{
if (shuru.size()==0)
{
return;
}
std::set<T*> ::iterator pt=shuru.begin();
//集中释放资源(不是曲线类的对象)
for (;pt!=shuru.end();pt++)
{
if ((*pt)!=0)
{
(*pt)->close();
}
}
}
 //功能:根据指针copy对象组
template<class T>
Acad::ErrorStatus  clone_entity( std::vector< T * > & export_enty,const std::vector< T * > & import_enty)
{
Acad::ErrorStatus es=Acad::eOk;
export_enty.clear();
export_enty.reserve(import_enty.size());
for ( std::vector< T * >::const_iterator begin=import_enty.begin();begin!=import_enty.end();begin++)
{
if ((*begin)==0)
{
continue;
}
T * linshi=0;
linshi=T::cast((*begin)->clone());
if(linshi!=0)
export_enty.push_back(linshi);
}
return es;
}
template<class T>
Acad::ErrorStatus  clone_entity( std::set< T * > & export_enty,const std::set< T * > & import_enty)
{
Acad::ErrorStatus es=Acad::eOk;
export_enty.clear();
for ( std::set< T * >::const_iterator begin=import_enty.begin();begin!=import_enty.end();begin++)
{
if ((*begin)==0)
{
continue;
}
T * linshi=0;
linshi=T::cast((*begin)->clone());
if(linshi!=0)
export_enty.insert(linshi);
}
return es;
}
 template<class T>
Acad::ErrorStatus  clone_entity( std::set< T * > & export_enty,const std::vector< T * > & import_enty)
{
Acad::ErrorStatus es=Acad::eOk;
export_enty.clear();
for ( std::vector< T * >::const_iterator begin=import_enty.begin();begin!=import_enty.end();begin++)
{
if ((*begin)==0)
{
continue;
}
T * linshi=0;
linshi=T::cast((*begin)->clone());
if(linshi!=0)
export_enty.insert(linshi);
}
return es;
}
//功能:根据id  copy指定类型的对象组,以可写的形式获得资源
template<class T>
Acad::ErrorStatus  clone_entity( std::vector< T * > & export_enty,const std::vector< AcDbObjectId > & import)
{
Acad::ErrorStatus es=Acad::eOk;
export_enty.clear();
export_enty.reserve(import.length());
for (int i=0;i<import.length();i++)
{

AcDbEntity *pEntity=0;
es=acdbOpenObject(pEntity, import[i],AcDb::kForRead);//以可写的形式获得资源//也可以用可读形式
if (pEntity==0)
{
continue;
}
if (pEntity->isKindOf(T::desc()))
{
T * linshi=0;
linshi=(T *)pEntity->clone();
if(linshi!=0)
  export_enty.push_back(linshi);
}
pEntity->close();
}
return es;
}

//功能:根据id  copy指定类型的对象组,以可写的形式获得资源
template<class T>
Acad::ErrorStatus  clone_entity( std::vector< T * > & export_enty,const AcDbObjectIdArray & import)
{
Acad::ErrorStatus es=Acad::eOk;
export_enty.clear();
export_enty.reserve(import.length());
for (int i=0;i<import.length();i++)
{
AcDbEntity *pEntity=0;
es=acdbOpenObject(pEntity, import.at(i),AcDb::kForWrite);//以可写的形式获得资源
if (pEntity==0)
{
continue;
}
if (pEntity->isKindOf(T::desc()))
{
T * linshi=0;
linshi=(T *)pEntity->clone();
if(linshi!=0)
export_enty.push_back(linshi);
}
pEntity->close();
}
return es;
}

template<class T>
Acad::ErrorStatus  change_AcDbObjectId_to_AcDbEntity(std::vector< T* > &fanhui_object_pt_vector,
const AcDbObjectIdArray &object_id,bool add,bool write)
{
Acad::ErrorStatus es=Acad::eOk;
if(add!=1) 
fanhui_object_pt_vector.clear();//不累加到fanhui_object_pt_vector
if(object_id.length()==0) 
{
return Acad::eInvalidInput; 

}
//acDocManager->lockDocument(get_database_AcApDocument(object_id.at(0).database()),AcAp::kWrite);//锁定
std::vector< T* > linshi;
for (int i=0;i<object_id.length();i++)
{
AcDbEntity *pEntity=0;
if(write==1)
es=acdbOpenObject(pEntity, object_id.at(i),AcDb::kForWrite);//以可写的形式获得资源
else
es=acdbOpenObject(pEntity, object_id.at(i),AcDb::kForRead);//以可写的形式获得资源
if(pEntity!=0)
   linshi.push_back((T*)pEntity);

}
fanhui_object_pt_vector.insert(fanhui_object_pt_vector.begin(),linshi.begin(),linshi.end());
//acDocManager->unlockDocument(get_database_AcApDocument(object_id.at(0).database()));//解除上面的锁定
return es;
}
/*==========================================================
//功能:通过id获得实体指针,过程失败会自动释放资源,fanhui_object_pt_vector设定为空
//参数:fanhui_object_pt_vector 为一个空的std::vector< AcDbEntity* > ,object_id为输入的id向量
//返回:
==========================================================*/
//根据id以读的形式打开资源
template<class T>
Acad::ErrorStatus  change_AcDbObjectId_to_AcDbEntity(std::set< T* > &fanhui_object_pt_vector,
const AcDbObjectIdArray &object_id,bool add,bool write)
{
Acad::ErrorStatus es=Acad::eOk;
if(add!=1) 
fanhui_object_pt_vector.clear();//不累加到fanhui_object_pt_vector
if(object_id.length()==0) 
{
return Acad::eInvalidInput; 

}
acDocManager->lockDocument(get_database_AcApDocument(object_id.at(0).database()),AcAp::kWrite);//锁定//隐含了一个问题
std::set< T* > linshi;
for (int i=0;i<object_id.length();i++)
{
AcDbEntity *pEntity=0;
if(write==1)
es=acdbOpenObject(pEntity, object_id.at(i),AcDb::kForWrite);//以可写的形式获得资源
else
es=acdbOpenObject(pEntity, object_id.at(i),AcDb::kForRead);//以可写的形式获得资源
if(pEntity!=0)
   linshi.insert((T*)pEntity);
}
fanhui_object_pt_vector.insert(linshi.begin(),linshi.end());
acDocManager->unlockDocument(get_database_AcApDocument(object_id.at(0).database()));//解除上面的锁定
return es;
}
 template<class T>
Acad::ErrorStatus  change_AcDbObjectId_to_AcDbEntity(T*  &fanhui_object,
AcDbObjectId &object_id,bool write)
{
Acad::ErrorStatus es=Acad::eOk;
acDocManager->lockDocument(get_database_AcApDocument(object_id.database()),AcAp::kWrite);//锁定
AcDbEntity *pEntity=0;
if(write==1)
es=acdbOpenObject(pEntity, object_id,AcDb::kForWrite);//以可写的形式获得资源
else
es=acdbOpenObject(pEntity, object_id,AcDb::kForRead);//以可写的形式获得资源
if(pEntity!=0)
  fanhui_object=(T*)pEntity;
acDocManager->unlockDocument(get_database_AcApDocument(object_id.database()));//解除上面的锁定
return es;
}
//获取id并释放资源//
template<class T>
int change_AcDbEntity_to_AcDbObjectId(AcDbObjectIdArray &object_id,std::set< T* > &fanhui_object_pt_vector)
{
object_id.removeAll();
int count=0;
std::set< T* >::iterator begin=fanhui_object_pt_vector.begin();
bool ok=0;
for (;begin!=fanhui_object_pt_vector.end();begin++)
{
if ((*begin)!=0)
{
 
if ((*begin)->id()==0)//输入的对象有没有id的
{
ok=1;
count++;
}
else
{
object_id.append((*begin)->objectId());
(*begin)->close();
(*begin)=0;
}
}
else
{
count++;
}
}
if (ok==1)
{
acutPrintf(_T("\nchange_AcDbEntity_to_AcDbObjectId出现一个问题,fanhui_object_pt_vector中存在%d个无效或未加入模型空间的实体,需要自行处理"),count);
}
return count;
}
//获取id并释放资源
template<class T>
int change_AcDbEntity_to_AcDbObjectId(AcDbObjectIdArray &object_id,std::vector< T* > &fanhui_object_pt_vector)
{
object_id.removeAll();
int count=0;
std::vector< T* >::iterator begin=fanhui_object_pt_vector.begin();
bool ok=0;
for (;begin!=fanhui_object_pt_vector.end();begin++)
{
if ((*begin)!=0)
{

if ((*begin)->id()==0)//输入的对象有没有id的
{
ok=1;
count++;
}
else
{
object_id.append((*begin)->objectId());
(*begin)->close();
(*begin)=0;
}

}
else
{
count++;
}
}
if (ok==1)
{
acutPrintf(_T("\nchange_AcDbEntity_to_AcDbObjectId出现一个问题,fanhui_object_pt_vector中存在%d个无效或未加入模型空间的实体,需要自行处理"),count);
}
return count;
}
 }
#endif


//实现文件////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "function_for_cad_1.h"
#include "..\..\..\cad2012+arx\函数库\geometry_lib\geometry_lib.h"
#include "..\..\..\调试检测类\ConsoleWindow\ConsoleWindow.h"
#include "..\..\..\文件系统\XTCOW_File\XTCOW_File.h"
using namespace xtcow;
namespace xtcow
{
std::wstring get_active_database_tile_name(void)
{
AcDbDatabase* active_file_AcDbDatabase=0;
active_file_AcDbDatabase=acdbHostApplicationServices()->workingDatabase();
if (active_file_AcDbDatabase==0)
{
acutPrintf(_T("\nacdbHostApplicationServices()->workingDatabase()失败,请检查"));
return _T("?");
}
return  get_database_tile_name(active_file_AcDbDatabase);

}
std::wstring get_database_tile_name(AcDbDatabase* input_database)
{
       if (input_database==0)
       {
acutPrintf(_T("\ninput_database为空,请检查"));
return _T("?");
       }
       int file_count=acDocManager->documentCount();
if (file_count==0)
{
return _T("空");
}
AcApDocumentIterator* iter = acDocManager->newAcApDocumentIterator();
AcApDocument* pDoc = 0;
XTCOW_File file;
while(!iter->done()) 
{//记录还剩哪些AcDbDatabase
pDoc= iter->document();
AcDbDatabase* AcDbDatabase_=pDoc->database();
std::wstring title_name=pDoc->docTitle();
if(AcDbDatabase_==input_database)
{
delete iter;
file.Assign(title_name);
return file.GetFileName();
}
iter->step();
}
delete iter;
return _T("?");
}
AcApDocument*  get_database_AcApDocument(AcDbDatabase* input_database)
{
if (input_database==0)
{
acutPrintf(_T("\ninput_database为空,请检查"));
return 0;
}
       int file_count=acDocManager->documentCount();
AcApDocumentIterator* iter = acDocManager->newAcApDocumentIterator();
AcApDocument* pDoc = 0;
while(!iter->done()) 
{//记录还剩哪些AcDbDatabase
pDoc= iter->document();
AcDbDatabase* AcDbDatabase_=pDoc->database();
if(AcDbDatabase_==input_database)
{
delete iter;
return pDoc;
}
iter->step();
}
delete iter;
return 0;
}
//功能:根据id  copy指定类型的对象组,以可写的形式获得资源
Acad::ErrorStatus  clone_entity( AcDbEntity *&export_enty,const AcDbObjectId import)
{
if(export_enty!=0)
{
acutPrintf(_T("\n接受克隆对象的指针非空,请检查"));
return Acad::eInvalidInput;
}
Acad::ErrorStatus es=Acad::eOk;
AcDbEntity *pEntity=0;
es=acdbOpenObject(pEntity, import,AcDb::kForRead);//以可读的形式获得资源
if (pEntity!=0)
{
export_enty=(AcDbEntity*)pEntity->clone();
pEntity->close();
}
return es;
}
bool is_showed(AcDbEntity *pEnt)//不同于cad_block类的
{
if(pEnt==0)
return 0;
if(pEnt->visibility()==kInvisible) 
return 0;
return 1;
}
void throw_error(Acad::ErrorStatus err)//抛出异常
{
if(err!=Acad::eOk) 
throw err;
}
void change_cad_title(std::wstring title)
{
CMDIFrameWnd *pp=0;
pp=acedGetAcadFrame();
if (pp==0)
{
acutPrintf(_T("\nchange_cad_title失败,请检查"));
return ;
}
pp->SetWindowText (title.c_str());
pp->UpdateWindow ();
}

void cad_zoom()//借用网上的
{
// get the extents of the drawing
AcDbViewTableRecord view;
AcGePoint3d max =
acdbHostApplicationServices()->workingDatabase()->extmax(),
min = acdbHostApplicationServices()->workingDatabase()->extmin();
AcGePoint2d max_2d (max[X], max[Y]);
AcGePoint2d min_2d (min[X], min[Y]);
// now set the view centre point
view.setCenterPoint (min_2d + (max_2d - min_2d) / 2.0);
// now height and width of view
view.setHeight(max_2d[Y] - min_2d[Y]);
view.setWidth (max_2d[X] - min_2d[X]);
// set the view
acedSetCurrentView (&view, NULL);
// updates the extents
acdbHostApplicationServices()->workingDatabase()->updateExt(TRUE);
}
void cad_zoom(AcGePoint2d CenterPt,double Height,double Width)//借用网上的
{
Acad::ErrorStatus es=Acad::eOk;
AcDbViewTableRecord *view=new AcDbViewTableRecord();
view->setCenterPoint(CenterPt);
view->setFrontClipAtEye(false);
view->setHeight(Height);
view->setWidth(Width);
es=acedSetCurrentView(view,NULL);
view->close();
delete view;
}
void cad_zoom_win()//借用网上的
{
AcGePoint3d max, min;
// get the window coords
int res = acedGetPoint (NULL, _T("点击放大范围的第一点:\n"),asDblArray(min));
// if ok
if (res == RTNORM)
{
res = acedGetCorner (asDblArray(min),  _T("点击放大范围的第二点:\n"),asDblArray(max));
if ((min.x>max.x)&&(min.y>max.y))
{
std::swap(min,max);
}
if ((min.x<max.x)&&(min.y>max.y))
{
std::swap(min.y,max.y);
}
if ((min.x>max.x)&&(min.y<max.y))
{
std::swap(min.x,max.x);
}
// get the extents of the drawing
AcDbViewTableRecord view;
AcGePoint2d max_2d (max[X], max[Y]);
AcGePoint2d min_2d (min[X], min[Y]);
// now set the view centre point
view.setCenterPoint (min_2d + (max_2d - min_2d) / 2.0);
// now height and width of view
view.setHeight(max_2d[Y] - min_2d[Y]);
view.setWidth (max_2d[X] - min_2d[X]);
// set the view
acedSetCurrentView (&view, NULL);
// updates the extents
acdbHostApplicationServices()->workingDatabase()->updateExt(TRUE);
}
}
//时间库
std::wstring printf_sys_time(std::wstring text)
{
//SYSTEMTIME sys; 
//GetLocalTime( &sys ); 
//wchar_t time[100];
//wsprintf(time,_T("%s开始于%d小时:%d分:%d秒\n"),text.c_str(),sys.wHour,sys.wMinute,sys.wSecond); //time含换行符号
//DBWindowWrite(time);
//return std::wstring(time);
return _T("");
}
std::wstring get_cad_file_path_and_name(void)
{
AcDbDatabase * pDb=0;
   pDb = acdbCurDwg();
if (pDb==0)
{
acutPrintf(_T("\nacdbCurDwg失败,请检查"));
return _T("?");
}
AcApDocument * pDoc =0;
pDoc= acDocManager->document(pDb);
if (pDoc==0)
{
acutPrintf(_T("\nacDocManager->document失败,请检查"));
return _T("?");
}
return pDoc->fileName();
}
void SendCmdToAcad(std::wstring &Cmd)
{
acedGetAcadTextCmdLine()->SendMessage(WM_CHAR,VK_ESCAPE,0);
acDocManager->sendStringToExecute(acDocManager->curDocument(),Cmd.c_str(),false,true);
}
bool AddSupportPath(std::vector<std::wstring> &arPath)//借用网上
{
IAcadPreferences *pPrefer=0;
IAcadPreferencesFiles *pPreferFiles=0;
HRESULT hr = NOERROR;
TRY
{
LPDISPATCH pAcadDisp =acedGetAcadWinApp()->GetIDispatch(TRUE);
IAcadApplication *pAcad = 0;
if (SUCCEEDED(pAcadDisp->QueryInterface(IID_IAcadApplication,(void**)&pAcad)))
{
BSTR supportPath;
hr = pAcad->get_Preferences(&pPrefer);
hr = pPrefer->get_Files(&pPreferFiles);
hr = pPreferFiles->get_SupportPath(&supportPath);
CString stemp = supportPath;
for (int i = 0; i< arPath.size(); i++)
{
CString ss = arPath[i].c_str();
stemp = stemp + _T(";") + ss;
}
supportPath = stemp.AllocSysString();
pPreferFiles->put_SupportPath(supportPath);
SysFreeString(supportPath);// 用完释放
}
pPrefer->Release();
pPreferFiles->Release();
pAcad->Release();
}
CATCH(COleDispatchException,e)
{
e->ReportError();
e->Delete();
return FALSE;
}
END_CATCH;
return TRUE;
}
//需要输入cad对象指针
void close_entity(AcGeVoidPointerArray &shuru)
{
if (shuru.length()==0)
{
return;
}
//集中释放资源
for (int i=0;i<shuru.length();i++)
{
if (shuru[i]!=0)
{
((AcDbEntity*)shuru[i])->close();//cad好像有个特性,重复调用close()不会出错
}
}
}
//************************************************************************
//函数名称:get_AcDbBlockTableRecord//借用网上资料
//返回值:Acad::ErrorStatus
//功能描述:打开当前空间获得AcDbBlockTableRecord ||返回的pSpaceRecord资源要自己释放||将实体加入一个新建的数据库中
//变量名:pSpaceRecord    变量类型:AcDbBlockTableRecord *&   变量说明:传递一个空的指针引用,以此为接口获得资源
//变量名:pDb     变量类型:AcDbDatabase*    变量说明:文件数据库
//变量名:mode         变量类型:AcDb::OpenMode   变量说明:打开模式
//变量名:nModelSpace     变量类型:bool                   变量说明:
// 使用方式:
//************************************************************************
AcDbObjectId get_AcDbBlockTableRecord(AcDbBlockTableRecord *&pSpaceRecord, 
std::wstring block_name,
AcDb::OpenMode mode,
AcDbDatabase* pDb)
{
AcDbObjectId id;
if (pDb==0)//
{
pDb=acdbHostApplicationServices()->workingDatabase() ;
}
Acad::ErrorStatus es=Acad::eOk; 
AcDbBlockTable*  pBlockTable=0;
#ifdef ACADR14
es=pDb->getBlockTable(pBlockTable,AcDb::kForRead);
#else
es=pDb->getSymbolTable(pBlockTable,AcDb::kForRead);
#endif
//AcDbBlockTable是AcDbBlockTableRecords的符号表,表示一个图形数据库中的块定义。
//AcDbBlockTableRecord*  pSpaceRecord=NULL;//AcDbBlockTableRecord类的对象作为图形文件数据库中的实体的容器。
//AcDbBlocktableRecord对象(通常指BTRs)被数据库的AcDbBlockTable对象拥有。
if (es==Acad::eOk)//成功获得pBlockTable资源
{
if ((es = pBlockTable->getAt(block_name.c_str(), pSpaceRecord, mode))==Acad::eOk)//成功获得pSpaceRecord资源
{
  es=pBlockTable->close();
  if (es!=Acad::eOk)
  {
acutPrintf(_T("BlockTable释放失败,错误码为:%d\n"),acadErrorStatusText(es));
pSpaceRecord->close();//BlockTable释放失败,那么将已经获得的pSpaceRecord也释放
  }
}
else//获得pSpaceRecord资源失败
{
  es=pBlockTable->close();
  if (es!=Acad::eOk)
  {
acutPrintf(_T("BlockTable释放失败,错误码为:%d\n"),acadErrorStatusText(es));
  }

}
return pSpaceRecord->id();
        
}
return id;
}
Acad::ErrorStatus make_entity_ForRead(AcDbVoidPtrArray& curveSegments)
{
Acad::ErrorStatus es=Acad::eOk;

for (int count=0;count<curveSegments.length();count++)
{
if (curveSegments[count]==0)
{
continue;
}
if (((AcDbEntity*)curveSegments[count])->isWriteEnabled())//这里似乎和kk=((AcDbEntity*)curveSegments[count])->id()一个作用,忘了,但能正常运行
{
AcDbObjectId kk=((AcDbEntity*)curveSegments[count])->id();
if (kk==0)
{
continue;
}
((AcDbEntity*)curveSegments[count])->close();
//
AcDbEntity*T=0;
es=acdbOpenObject(T,kk,AcDb::kForRead,AcDbEntity::desc());
if(T!=0) 
{
curveSegments[count]=T;
}
else
{
curveSegments[count]=T;
acutPrintf(_T("\nmake_entity_ForRead出现一个问题"));
}

}
}
return es;
}
Acad::ErrorStatus make_entity_ForWrite(AcDbVoidPtrArray& curveSegments)
{
Acad::ErrorStatus es=Acad::eOk;
for (int count=0;count<curveSegments.length();count++)
{
if (curveSegments[count]==0)
{
continue;
}
if (((AcDbEntity*)curveSegments[count])->isReadEnabled())
{
AcDbObjectId kk=((AcDbEntity*)curveSegments[count])->id();
if (kk==0)
{
continue;
}
((AcDbEntity*)curveSegments[count])->close();
AcDbEntity*T=0;
es=acdbOpenObject(T,kk,AcDb::kForWrite,AcDbEntity::desc());
if(T!=0) 
{
curveSegments[count]=T;
}
else
{
curveSegments[count]=T;
acutPrintf(_T("\nmake_entity_ForWrite出现一个问题"));
}
}
}
return es;
}
//如果存在该层,则通过AcDbObjectId返回该层id,可以对AcDbDatabase* pDb输入NULL
bool has_the_same_lay(std::wstring inputLyName,AcDbDatabase* pDb)
{
Acad::ErrorStatus es=Acad::eOk; 
if (pDb==0)
{
pDb=acdbHostApplicationServices()->workingDatabase();
if(pDb==0)
{
acutPrintf(_T("\nacdbHostApplicationServices()->workingDatabase()失败"));
return 0;
}
}

AcApDocument* pDoc=0;
pDoc= acDocManager->curDocument();//返回当期打开的图形相关联的AcApDocument对象
if(pDoc==0)
{
acutPrintf(_T("\nacDocManager->curDocument()失败"));
return 0;
}

// 获得当前图形数据库的符号表
AcDbLayerTable  *pLayerTable=0;
if((es=pDb->getLayerTable(pLayerTable,AcDb::kForWrite))!=Acad::eOk)
{
acutPrintf(_T("getLayerTable失败\n"));
return 0;
//
AcDbLayerTableIterator * pBlockIterator=0;
es=pLayerTable->newIterator(pBlockIterator);
AcDbLayerTableRecord *pLayerTableRcd=0;
wchar_t *pLtName=0;
//对获得的块进行遍历
for (; !pBlockIterator->done(); pBlockIterator->step())
{
//获得块中实体
es=pBlockIterator->getRecord(pLayerTableRcd, AcDb::kForRead);
if (es!=Acad::eOk)
{
continue;
}
pLayerTableRcd->getName(pLtName);
//找到同名的层
if (!_tcscmp(pLtName,inputLyName.c_str()))//当s1==s2时,返回值=0
{
//ObjectId=pLayerTableRcd->id();
pLayerTableRcd->close();
pLayerTable->close();
delete pBlockIterator;
return 1;
}
else
{
pLayerTableRcd->close();
}
}
// 关闭图层表和图层表记录
delete pBlockIterator;
pLayerTable->close();
if (es!=Acad::eOk)
{
acutPrintf(_T("has_the_same_lay出现问题\n"));
}
return  0;
}
bool creat_new_layer(std::wstring inputLyName,AcDbDatabase* pDb)
{
Acad::ErrorStatus es=Acad::eOk; 
if (pDb==0)
{
pDb=acdbHostApplicationServices()->workingDatabase();
if(pDb==0)
{
acutPrintf(_T("\nacdbHostApplicationServices()->workingDatabase()失败"));
return 0;
}
}
// 获得当前图形数据库的符号表
AcDbLayerTable  *pLayerTable=0;
acDocManager->lockDocument(acDocManager->curDocument(),AcAp::kWrite);//解
if((es=pDb->getLayerTable(pLayerTable,AcDb::kForWrite))!=Acad::eOk)
{
acDocManager->unlockDocument(acDocManager->curDocument());//解除上面的锁定
 acutPrintf(_T("\ngetLayerTable失败"));
return 0;
}
// 生成新的图层表记录
AcDbLayerTableRecord  *pLayerTableRecord = new AcDbLayerTableRecord;
if ((es=pLayerTableRecord->setName(inputLyName.c_str()))!=Acad::eOk) // 设置图层名
{
es=pLayerTable->close();
es=pLayerTableRecord->close();
acDocManager->unlockDocument(acDocManager->curDocument());//解除上面的锁定
acutPrintf(_T("\ncreat_new_layer失败,可能存在同名图层"));
return 0;
}
//将新建图层加入图层记录表
AcDbObjectId ObjectId;
es=pLayerTable->add(ObjectId,pLayerTableRecord);
// 关闭图层表和图层表记录
es=pLayerTableRecord->close();
es=pLayerTable->close();
if (es!=Acad::eOk)
{
acutPrintf(_T("\ncreat_new_layer出现问题"));
}
acDocManager->unlockDocument(acDocManager->curDocument());//解除上面的锁定
return  es;
}
bool has_the_same_block(std::wstring BlockName, AcDbDatabase *pDb)
{
Acad::ErrorStatus es=Acad::eOk;
if (pDb==0)
{
pDb=acdbHostApplicationServices()->workingDatabase();
if(pDb==0)
{
acutPrintf(_T("\nacdbHostApplicationServices()->workingDatabase()失败"));
return 0;
}
}
AcDbBlockTable *pTable = 0;
if ((es=pDb->getBlockTable(pTable, AcDb::kForRead))!=Acad::eOk)
{
acutPrintf(_T("\ngetBlockTable出现问题"));
return 0;
}
bool i=pTable->has(BlockName.c_str());
pTable->close();
return i;
}
//借用网上资料
std::wstring get_a_different_block_name_if_input_name_had_exited(std::wstring BlockName, AcDbDatabase *i_pDb)
{
Acad::ErrorStatus es=Acad::eOk;
AcDbBlockTable *pTable = 0;
if ((es=i_pDb->getBlockTable(pTable, AcDb::kForRead))!=Acad::eOk)
{
acutPrintf(_T("getBlockTable出现问题\n"));
return _T("getBlockTable出现问题\n");
}
//
int nName=-1;
std::wstring hhh;
do
{
hhh.clear();
hhh.append(BlockName);
hhh.append(_T("_"));
nName++;
hhh.append(int_to_wstring(nName));
}while(pTable->has(hhh.c_str()));
pTable->close();
return hhh;
}
}



路过

雷人

握手

鲜花

鸡蛋

评论 (0 个评论)

facelist doodle 涂鸦板

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

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

GMT+8, 2024-5-12 01:08 , Processed in 0.137035 second(s), 15 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部