⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 publickit.cpp

📁 这是书上的代码
💻 CPP
字号:
#include "StdAfx.h"
#include "publickit.h"
#include "acdb.h"
#include "adslib.h"
#include "dbsymtb.h"
#include "adeskabb.h"
#include "dbpl.h"
#include "gepnt2d.h"
#include "gept2dar.H"
#include "dbents.h "
#include "dbdict.h "
#include "dbgroup.h "
#include "dbxrecrd.h "

//时间:5月4日20:02
BOOL check_stringvalid(CString *str)
{ //检查输入字串的合理性,主要是看是否有空格字符
	long len;
	char c;
	len=str->GetLength();
	for(int i=0;i<len;i++)
	{
	c=str->GetAt(i);
	if(c==' ') 
	return FALSE;
	}
	return TRUE;
}

BOOL check_groupname_exit(CString *str)
{//检查给定的组名在当前图形文件中是否存在
	BOOL re;
	AcDbDatabase *pDb;
	AcDbDictionary *pGroupDict;
	pDb=acdbHostApplicationServices()->workingDatabase();
	if(pDb==NULL)
	{
		AfxMessageBox("未打开图形文件");
		return FALSE;
	}
	pDb->getGroupDictionary(pGroupDict,AcDb::kForRead);
	re=pGroupDict->has(str->GetBuffer(1));
	pGroupDict->close();
	return re;
}

BOOL has_app()
{//判断当前图形文件是否已创建铸造工程
	CString str="Cast_App_Name";
	return check_groupname_exit(& str);
}

BOOL enum_groupname(CString *str,CList<CString,CString> *strlist)
{//枚举当前图形文件中具有指定特征的组名
	CString                tempstr,temp;
	AcDbDatabase           *pDb;
	AcDbDictionary         *pGroupDict;
	AcDbDictionaryIterator *pDictIterator;
	AcDbGroup              *pGroup;
	if(str==NULL||strlist==NULL)
		return FALSE;
	pDb=acdbHostApplicationServices()->workingDatabase();
	if(pDb==NULL)
	{
		AfxMessageBox("未打开图形文件");
		return FALSE;
	}
	pDb->getGroupDictionary(pGroupDict,AcDb::kForRead);
	pDictIterator=pGroupDict->newIterator(AcRx::kDictCollated);
	for(;!pDictIterator->done();pDictIterator->next())
	{//又可能要用一个AcDbObject对象作中间转换
		AcDbObject * pp;
		pDictIterator->getObject(pp,AcDb::kForWrite);
		pGroup=(AcDbGroup *)pp;
		tempstr=pGroup->name();
		if(str->GetLength()<=tempstr.GetLength())
		{
		if(tempstr.Left(str->GetLength())==*str)
		{
			if(tempstr.Right(7)=="oversee")
			{
				pGroup->close();
				continue;
			}
			if(tempstr.Right(4)=="left")
			{
				pGroup->close();
				continue;
			}
			strlist->AddTail(tempstr.Right(tempstr.GetLength()-str->GetLength()));
			
		}
		}
		pGroup->close();
	}
	delete pDictIterator;
	pGroupDict->close();
	return TRUE;
}


BOOL get_xdata(int n,rexdata *re,AcDbGroup *pGroup)
{//按给定的组名获取指定位置的扩展数据
	if(pGroup==NULL||n<0||re==NULL)
		return FALSE;
    AcDbDictionary *pxDict;
	AcDbXrecord    *pXrec;
	AcDbObjectId  groupId,dictId;
	CString sxname=pGroup->name();
	sxname+="info";
	struct resbuf *pTemp,*pr;
		
	dictId=pGroup->extensionDictionary();
	acdbOpenObject(pxDict,dictId,AcDb::kForRead);
	if(pxDict==NULL) 
		return FALSE;
	pxDict->getAt(sxname.GetBuffer(1),(AcDbObject * &)pXrec,
		AcDb::kForRead);
	pxDict->close();
	pXrec->rbChain(&pr);
	pXrec->close();
	pTemp=pr;
	for(int i=0;i<n;i++)
		pTemp=pTemp->rbnext;
	if(n==0)
	{//字符串
		re->str=pTemp->resval.rstring;
		return TRUE;
	}
	if(n>0&&n<4)
	{//整型
        re->int_value=pTemp->resval.rint;
		return TRUE;
	}
	if(n>=4&&n<7)
	{//长整型
        re->long_value=pTemp->resval.rlong;
		return TRUE;
	}        
	if(n>=7)//实型
        re->double_value=pTemp->resval.rreal;
	return TRUE;
	
}



BOOL set_xdata(int n,rexdata *re,AcDbGroup *pGroup)
{//修改组的指定位置的扩展数据
	if(pGroup==NULL) return FALSE;

	if(n<0||re==NULL)
		return FALSE;
    AcDbDictionary *pxDict;
	AcDbXrecord    *pXrec;
	AcDbObjectId  groupId,dictId;
	CString sxname;
	sxname=pGroup->name();
	sxname+="info";//扩展数据名

	struct resbuf *pTemp,*pr;
	dictId=pGroup->extensionDictionary();
    acdbOpenObject(pxDict,dictId,AcDb::kForRead);
	if(pxDict==NULL) 
		return FALSE;
	pxDict->getAt(sxname.GetBuffer(1),(AcDbObject * &)pXrec,
	            	AcDb::kForWrite);
	pxDict->close();
	pXrec->rbChain(&pr);
	pTemp=pr;
	for(int i=0;i<n;i++)
		pTemp=pTemp->rbnext;
	if(n==0)//字符串
        pTemp->resval.rstring=re->str;
	
	if(n>0&&n<4)//整型
        pTemp->resval.rint=re->int_value;
	
	if(n>=4&&n<7)//长整型
        pTemp->resval.rlong=re->long_value;
	
	if(n>=7)//实型
        pTemp->resval.rreal=re->double_value;
	pXrec->setFromRbChain(*pr);
	pXrec->close();
	return TRUE;
	
}	


BOOL create_group(CString *str,struct resbuf *pr)
{//创建一个组
	AcDbDatabase *pDb;
	AcDbDictionary *pGroupDict;
	if(str==NULL) 
		return FALSE;
    AcDbGroup *pGroup=new AcDbGroup(str->GetBuffer(1));
	
	AcDbObjectId groupId,diId,XrId;
	
	pDb=acdbHostApplicationServices()->workingDatabase();
	pDb->getGroupDictionary(pGroupDict,AcDb::kForWrite);
	
	pGroupDict->setAt(str->GetBuffer(1),pGroup,groupId);
	pGroupDict->close();
	pGroup->close();
	CString sxname=*str;
	sxname+="info";
	if(pr!=NULL)
	{
		AcDbXrecord *pXrec=new AcDbXrecord;
		acdbOpenObject(pGroup,groupId,AcDb::kForWrite);
		pGroup->createExtensionDictionary();
		diId=pGroup->extensionDictionary();
		pGroup->close();
		
		acdbOpenObject(pGroupDict,diId,AcDb::kForWrite);
		pGroupDict->setAt(sxname.GetBuffer(1),pXrec,XrId);
		pGroupDict->close();
		if(pXrec->setFromRbChain(*pr)==Acad::eOk)
		pXrec->close();
	}
	return TRUE;
}

BOOL open_group(AcDbGroup *& pGroup,CString *s)
{//打开一个组用于添加实体
	AcDbDatabase *pDb;
	AcDbDictionary *pGroupDict;
	pDb=acdbHostApplicationServices()->workingDatabase();
	pDb->getGroupDictionary(pGroupDict,AcDb::kForWrite);
	
	AcDbObjectId id;
	if(pGroupDict->getAt(s->GetBuffer(1),id)!=Acad::eOk)
	{
		pGroupDict->close();
		return FALSE;
	}
	pGroupDict->close();
	if(acdbOpenObject(pGroup,id,AcDb::kForWrite)!=Acad::eOk)
		return FALSE;
	return TRUE;
}

BOOL delete_group(CString *str)
{
	if(str==NULL||str->IsEmpty())
		return TRUE;
	if(!check_groupname_exit(str))
		return TRUE;
	AcDbGroup     *pGroup;
	AcDbObject    *pObj;
	AcDbObjectId   id;
	AcDbGroupIterator *piterator;

	if(!open_group(pGroup,str))
	{
		if(pGroup!=NULL)
		{
			AfxMessageBox("打开租不成功,pGroup!=NULL");
		    pGroup->close();
		}
		AfxMessageBox(*str);
		return FALSE;
	}
	piterator=pGroup->newIterator();
     for(;!piterator->done();piterator->next())
	 {
		 piterator->getObject(pObj,AcDb::kForWrite);
         pObj->erase();
		 pObj->close();
	 }
	 delete piterator;
//此处删除扩展数据可能有问题????????????????
	 id=pGroup->extensionDictionary();
	 if(id!=AcDbObjectId::kNull)
	 {	
	 AcDbDictionary *pxDict;
	 CString sname;
	 sname==*str+"info";
	 if(acdbOpenObject(pxDict,id,AcDb::kForWrite)==Acad::eOk)
	 {
	 pxDict->remove(sname.GetBuffer(1));
	 pxDict->close();
	 }
	 pGroup->releaseExtensionDictionary();
	 }
	 pGroup->erase();
	 pGroup->close();
     return TRUE;  
}

BOOL select_layer(CString s)
{//选择一个层为当前层,并把层的线型设为当前线型
	AcDbDatabase *pDb;
	AcDbLayerTable *plt;
	AcDbLayerTableRecord *pltr;
	AcDbObject *ob;
	AcDbObjectId id;
	
	pDb=acdbHostApplicationServices()->workingDatabase();
	pDb->getLayerTable(plt,AcDb::kForRead);
	plt->getAt(s.GetBuffer(1),id);
	pDb->setClayer(id);//设置层
	acdbOpenObject(ob,id,AcDb::kForRead);
	pltr=(AcDbLayerTableRecord *)ob;
	id=pltr->linetypeObjectId();
	pDb->setCeltype(id);
	plt->close();
	pltr->close();
	
	return TRUE;
}

double get_sina1(double l,double R,double h)
{//用于计算三角函数值
	double a, b, c ,q,result;
	a=l*l+h*h;
	b=2*h*R;
	c=R*R-l*l;
	q=b*b-4*a*c;
	result=(b+sqrt(q))/(2*a);
	return result;
}

double get_sina2(double l,double R,double h)
{//用于计算三角函数值
	double a, b, c ,q,result;
	a=l*l+h*h;
	b=2*l*R;
	c=R*R-h*h;
	q=b*b-4*a*c;
	result=(b+sqrt(q))/(2*a);
	return result;
}

char *get_dimstring(double data,int type)
{  
	CString s;
	switch(type)
	{	case 0://标注一般直线
		
		s.Format("%.2f",data);
		return s.GetBuffer(1);
	case 1://标注半径
		s.Format("R%.2f",data);
		return s.GetBuffer(1);
	case 2://标注直径
		s.Format("%%c%.2f",data);
		return s.GetBuffer(1);
	case 3://标注误差
		s.Format("%%p%.2f",data);
		return s.GetBuffer(1); 
	case 4://标注度数
		s.Format("%%d%.2f",data);
		return s.GetBuffer(1); 
	}
	return NULL;
}
///////////////////////
//加载线形
void loadlinetype()
{
 AcDbDatabase *pb;
 AcDbLinetypeTable *pLinetypeTb,*pLinetypeTb1;
 AcDbLinetypeTableRecord *qq=new AcDbLinetypeTableRecord;
 AcDbLinetypeTableRecord *qq1=new AcDbLinetypeTableRecord;
 pb=acdbHostApplicationServices()->workingDatabase();
 
 //加载中心线
 pb->getLinetypeTable(pLinetypeTb,AcDb::kForRead);
if(!pLinetypeTb->has("center"))

{ pLinetypeTb->close();
	pb->getLinetypeTable(pLinetypeTb,AcDb::kForWrite);

	qq->setName("center");
	qq->setIsScaledToFit(FALSE);
	qq->setNumDashes(4);
	qq->setDashLengthAt(0,31.75);
	qq->setDashLengthAt(1,-6.35);
   qq->setDashLengthAt(2,6.35);
    qq->setDashLengthAt(3,-6.35); 
	qq->setPatternLength(38);
	qq->setComments("center");
	pLinetypeTb->add(qq);
	pLinetypeTb->close();
	qq->close();

}

//加载隐藏线
  pb->getLinetypeTable(pLinetypeTb1,AcDb::kForRead);
  if(!pLinetypeTb1->has("hidden"))
  { pLinetypeTb1->close();
	pb->getLinetypeTable(pLinetypeTb1,AcDb::kForWrite);
	qq1->setName("hidden");
	qq1->setIsScaledToFit(FALSE);
	qq1->setNumDashes(2);
	qq1->setDashLengthAt(0,12.7);
	qq1->setDashLengthAt(1,-6.35);
	qq1->setPatternLength(6.35);
	qq1->setComments("hidden");
	pLinetypeTb1->add(qq1);
	pLinetypeTb1->close();
    qq1->close();
  }
}
////////////////////////
//设置图层
void setlayer()
{
	char *layername[]={ "cast_dim_layer",
		                "cast_jm_c_layer",
						"cast_jm_x_layer",
						"cast_jm_v_layer",
						"cast_jm_cn_layer",
						"cast_sand_layer" };
	char *linename[]={"Continuous","cContinuous","Contionous","hidden","center","Continuous" };
	Adesk::UInt16 colorId[]={240,240,240,240,240,170 };

	AcDbDatabase *pDb;
	pDb=acdbHostApplicationServices()->workingDatabase();

	AcDbLayerTable *plyTable;
	AcDbLayerTableRecord *plyTblRecord;

	AcDbLinetypeTable *pltTable;

	AcDbObjectId layerId,lineId;

	AcCmColor color;
	
	pDb->getLayerTable(plyTable,AcDb::kForWrite);
	pDb->getLinetypeTable(pltTable,AcDb::kForRead);

	for(int i=0;i<6;i++)
	{
	int flag=0;
	if(plyTable->has(layername[i]))
	{
	plyTable->getAt(layername[i],plyTblRecord,AcDb::kForWrite,Adesk::kFalse);
	}
	else
	{
	plyTblRecord=new AcDbLayerTableRecord;
	plyTblRecord->setName(layername[i]);
	flag=1;
	}
	color.setColorIndex(colorId[i]);
	plyTblRecord->setColor(color);
	pltTable->getAt(linename[i],lineId);
	plyTblRecord->setLinetypeObjectId(lineId);
	if(flag) plyTable->add(plyTblRecord);
	plyTblRecord->close();
	}
	pltTable->close();
	plyTable->close();

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -