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

📄 compublic.cpp

📁 在AUTOCAD环境下沿路线插入图块
💻 CPP
📖 第 1 页 / 共 5 页
字号:
			}

			if (pLinetypeTbl -> getAt(cLineType,ltypeObjId) != Acad::eOk) {
				ads_printf("\n错误:获取线型ID错误!\n");
				pLinetypeTbl->close();
				pLayerTable->close();					//关闭层表指针
				pLayerTableRecord->close();
				return FALSE;
			}

		} 

		pLinetypeTbl->close();							//关闭线形表指针

		pLayerTableRecord->setLinetypeObjectId(ltypeObjId);

		if (pLayerTable->add(pLayerTableRecord) !=Acad::eOk) {
			pLayerTable->close();
			pLayerTableRecord->close();
			return FALSE;
		}
		pLayerTable->close();							//关闭层表指针
		pLayerTableRecord->close();
	} 
	else {
		pLayerTable->close();
		return 0;
	}

	return TRUE;
}


/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
//| 函数名:			设定层状态
//|					SetCurLayer()
//| 输入参数:		char* layername			层名
//|					int bLock				锁定状态
//|					int bFrozen				冻结状态
//|					BOOL bCur				是否置为当前层
//| 返回:			BOOL
//| 说明:			bLock		=1设为锁定;=0解除锁定;=-1不改变状态
//|					bFrozen		=1设为冻结;=0解除冻结;=-1不改变状态
//| 举例:			略
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
BOOL SetLayer(char* layername,int bLock,int bFrozen,BOOL bCur)
{
	AcDbLayerTable *pLayerTbl;
	AcDbObjectId layerId;

	if (acdbCurDwg()->
		getLayerTable(pLayerTbl,AcDb::kForRead) != Acad::eOk) {
		pLayerTbl->close();
		ads_printf("\n错误:打开层表出错!\n");
		return FALSE;
	}
	
	AcDbLayerTableRecord *pLayerRecord;
	
	if (pLayerTbl->getAt(layername,pLayerRecord,AcDb::kForWrite) != Acad::eOk) {
		pLayerTbl->close();
		ads_printf("\n错误:获取层表记录出错!\n");
		return FALSE;
	}										//获取Record


	if (bCur) {			
		if (pLayerTbl->getAt(layername,layerId,false) != Acad::eOk) {
			pLayerTbl->close();
			ads_printf("\n错误:获取层表记录出错!\n");
			return FALSE;
		}									//获取ID,用于设定当前层
		acdbCurDwg()->setClayer(layerId);
	}

	pLayerTbl -> close();	

	if (bLock != -1)	pLayerRecord->setIsLocked(bLock);
	if (bFrozen != -1)	pLayerRecord->setIsFrozen(bFrozen);
	
	pLayerRecord->close();

	return TRUE;
}



/*##############################################
  #											   #
  #  绘图操作函数  Modify 2002.4.10 by KFX     #
  #											   #
/*##############################################*/

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
//| 函数名:			制作块
//|					MakeBlock()
//| 输入参数:		ads_point Pt1 点位置
//|                 char* layname 所在层
//| 返回:			AcDbObjectId
//| 说明:			指定层名,建立点实体
//| 举例:			略
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
BOOL InsertDwgBlock(ads_point insertpt, double scale, double angle,CString m_formname)
{
	char filename[100], temp[100];
	strcpy(temp, m_formname);
	strlwr(temp);
//	if (ads_findfile(temp, filename) != RTNORM) return FALSE;

	ads_point basept;

	// 读取图形块
	AcDbDatabase *dwgBlock = new AcDbDatabase(Adesk::kFalse);
	Acad::ErrorStatus es;
	es = dwgBlock->readDwgFile("c:\\test.dwg");
	ads_printf("\nsssssssssssssssss");
	if (es != Acad::eOk) 
	{
		AfxMessageBox("sdd");
		delete dwgBlock;
		return FALSE;
	}
	ads_printf("\nsssssssssssssssss");
	AcGePoint3d inbase(basept[0], basept[1], 0.0);	
	dwgBlock->setInsbase(inbase);
	double oldscale = dwgBlock->dimscale();

	// 插入图形块	
	AcGeMatrix3d matrix, m1, m2, m3;
	matrix = m1 = m2 = m3 = AcGeMatrix3d::kIdentity;	
	AcGePoint3d base(insertpt[0], insertpt[1], 0.0);
	AcGePoint3d base2(insertpt[0], insertpt[1]+5, 0.0);
	AcGeLine3d line(base,base2);

	m1.setToScaling(scale/oldscale, base);
	AcGeVector3d axis(0, 0, 1);	              
	ads_printf("\nsssssssssssssssss");
	if(angle == 180.00)
	{
		m2.setToMirroring(line);
	
		AcGeVector3d vector(insertpt[0]-basept[0], insertpt[1]-basept[1], 0.0);
		m3.setToTranslation(vector);
		matrix = m1*m2*m3;
	}
	else
	{
		AcGeVector3d vector(insertpt[0]-basept[0], insertpt[1]-basept[1], 0.0);
		m3.setToTranslation(vector);
		matrix = m1*m3;
	}
	es = acdbCurDwg()->insert(matrix, dwgBlock);
	ads_printf("\nsssssssssssssssss");
	if (es != Acad::eOk) 
	{

		delete dwgBlock;
		return FALSE;
	}
//	delete dwgBlock;

	return TRUE;
}

AcDbObjectId insblock(char name[5],AcGePoint3d center,
					  int xy,double bl)
{
	double sx,sy,sz;
	switch(xy)
	{
	case 0:
		sx=1;
		sy=bl;
		sz=1;
		break;
	case 1:
		sx=bl;
		sy=1;
		sz=1;
		break;
	}
	AcGeScale3d xyzscalse(sx,sy,sz);
	AcDbBlockTable *pbtable;
	acdbCurDwg()->getBlockTable(pbtable,AcDb::kForRead);
	
	AcDbBlockTableRecord *pBlockTableRecord;
	if(Acad::eOk!=pbtable->getAt(name,pBlockTableRecord,AcDb::kForRead))
	{ 
		pbtable->close();
		return NULL;
	}
	pbtable->close();
	AcDbObjectId entityid;
	entityid=pBlockTableRecord->objectId();
	pBlockTableRecord->close();
	//插入块
	AcDbBlockReference *pblkref=new AcDbBlockReference;
	pblkref->setBlockTableRecord(entityid);
	pblkref->setPosition(center);
	pblkref->setRotation(0.0);
	pblkref->setScaleFactors(xyzscalse);
	AcDbBlockTable *pbt;
	acdbCurDwg()->getBlockTable(pbt,AcDb::kForRead);
	AcDbBlockTableRecord *pBTRecord;
	pbt->getAt(ACDB_MODEL_SPACE,pBTRecord,AcDb::kForWrite);
	pbt->close();
	AcDbObjectId newentityid;
	pBTRecord->appendAcDbEntity(newentityid,pblkref);
	pBTRecord->close();
	pblkref->close();
	return newentityid;
}


/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
//| 函数名:			绘制点
//|					MakePoint()
//| 输入参数:		ads_point Pt1 点位置
//|                 char* layname 所在层
//| 返回:			AcDbObjectId
//| 说明:			指定层名,建立点实体
//| 举例:			略
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
AcDbObjectId MakePoint(ads_point Pt1,char* layname)
{
	AcDbObjectId PointId;
	
	AcGePoint3d Pt((double)Pt1[0],(double)Pt1[1], 0.0);
	
	AcDbPoint *pPoint = new AcDbPoint(Pt);

	AcDbBlockTable *pBlockTable;
	
	if (acdbCurDwg()->
		getBlockTable(pBlockTable,AcDb::kForRead) != Acad::eOk) {
		ads_printf("\n错误:无法打开块表!\n");
		pPoint->close();
		return 0;
	}

	AcDbBlockTableRecord *pBlockTableRecord;

	if (pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord,
		AcDb::kForWrite) != Acad::eOk) {
		ads_printf("\n错误:无效块表!\n");
		pBlockTable->close();
		pPoint->close();
		return 0;
	}
   	if (pBlockTable->close() != Acad::eOk) {
		ads_printf("\n错误:不能关闭块表!\n");
		return 0;
	}

	if (pBlockTableRecord->appendAcDbEntity(PointId, pPoint) != Acad::eOk) {
		ads_printf("\n错误:不能填加实体至数据库!");
		pPoint->close();
		return 0;
	}

	if (pBlockTableRecord->close() != Acad::eOk) {
		ads_printf("\n错误:无法关闭符号表!\n");
		pPoint->close();
		return 0;
	}

	pPoint->setLayer(layname);
	pPoint->close();
    return PointId;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
//| 函数名:			绘制普通直线
//|					MakeLine()
//| 输入参数:		ads_point Pt1			起点
//|					ads_point Pt2			终点
//|					int col					颜色	
//|					char* layname			所在层
//|					struct resbuf *xData	填加扩展数据
//|					BOOL draw				是否绘制
//|
//| 返回:			AcDbObjectId
//| 说明:			指定层名,建立普通直线实体
//| 举例:			略
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
AcDbObjectId MakeLine(ads_point Pt1,ads_point Pt2,int col,CString layname,struct resbuf *xData,BOOL draw)
{
	AcDbObjectId lineId;

	AcGePoint3d startPt((double)Pt1[0],(double)Pt1[1], 0.0);
	AcGePoint3d endPt((double)Pt2[0],(double)Pt2[1], 0.0);

	if (draw) {
		AcDbBlockTable *pBlockTable;


		AcDbLine *pLine = new AcDbLine(startPt, endPt);

		if (acdbCurDwg()->
			getBlockTable(pBlockTable,AcDb::kForRead) != Acad::eOk) {
			ads_printf("\n错误:无法打开块表!\n");
			pLine->close();
			return 0;
		}
			

		AcDbBlockTableRecord *pBlockTableRecord;

		
		if (pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord,
			AcDb::kForWrite) != Acad::eOk) {
			ads_printf("\n错误:无法获取符号表!\n");
			pBlockTable->close();
			pLine->close();
			return 0;
		}


		if (pBlockTable->close() != Acad::eOk) {
			ads_printf("\n错误:无法关闭块表!\n");
			pBlockTableRecord->close();
			pLine->close();
			return 0;
		}


		if (pBlockTableRecord->appendAcDbEntity(lineId, pLine) != Acad::eOk) {
			ads_printf("\n错误:无法填加实体至数据库!\n");
			pBlockTableRecord->close();
			pLine->close();
			return 0;
		}			//将AcDbHatch对象添加到数据库中

		if (pBlockTableRecord->close() != Acad::eOk) {
			ads_printf("\n错误:无法关闭符号表!\n");
			pLine->close();
			return 0;
		}

		pLine->setColorIndex(col);
		pLine->setLayer(layname);

		//===========填加扩展数据===========		
		pLine->upgradeOpen();
		if (pLine->setXData(xData) != Acad::eOk){

			ads_printf("\n警告: 实体(直线 ID=%d)添加扩展数据时出错!\n",lineId);

			pLine->close();
			return lineId;
		}
		//==================================

		pLine->close();
	}
	else {
		ads_grdraw(Pt1,Pt2,col,0);
	}
    return lineId;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
//| 函数名:			绘制带线宽直线
//|					MakeLineEx()
//|						overload function
//| 输入参数:		ads_point Pt1			起点
//|					ads_point Pt2			终点
//|					double width			线宽
//|					int col					颜色	
//|					char* layname			所在层
//|					struct resbuf *xData	填加扩展数据
//|					BOOL draw				是否绘制
//|
//| 返回:			AcDbObjectId
//| 说明:			指定层名,建立带线宽的直线实体
//| 举例:			略
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
AcDbObjectId MakeLineEx(ads_point pt1,ads_point pt2,double width,
			int col,char* layname,struct resbuf *xData,BOOL draw)
{
	AcDbObjectId PlineId;

	AcGePoint3dArray ptArr;//定义三维坐标数组
	ptArr.setLogicalLength(2);
	ptArr[0].set(pt1[0],pt1[1],0.0);
	ptArr[1].set(pt2[0],pt2[1],0.0);

	if (draw) {
		AcDb2dPolyline *pLine = new AcDb2dPolyline(AcDb::k2dSimplePoly,
			ptArr,0.0,Adesk::kTrue,width,width);

		AcDbBlockTable *pBlockTable;
		
		if (acdbCurDwg()->
			getBlockTable(pBlockTable,AcDb::kForRead) != Acad::eOk) {
			ads_printf("\n错误:无法打开块表!\n");
			pLine->close();
			return 0;
		}


		AcDbBlockTableRecord *pBlockTableRecord;

		if (pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord,
			AcDb::kForWrite) != Acad::eOk) {
			ads_printf("\n错误:无法获取符号表!\n");
			pBlockTable->close();
			pLine->close();
			return 0;
		}

		
		if (pBlockTable->close() != Acad::eOk) {
			ads_printf("\n错误:无法关闭块表!\n");
			pBlockTableRecord->close();
			pLine->close();
			return 0;
		}


		pLine->setColorIndex(col);
		pLine->setLayer(layname);
		pLine->setDefaultStartWidth(width);
		pLine->setDefaultEndWidth(width);

		if (pBlockTableRecord->appendAcDbEntity(PlineId,pLine) != Acad::eOk) {
			ads_printf("\n错误:无法填加实体至数据库!\n");
			pBlockTableRecord->close();
			pLine->close();
			return 0;
		}			//将AcDbHatch对象添加到数据库中


		if (pBlockTableRecord->close() != Acad::eOk) {
			ads_printf("\n错误:无法关闭符号表!\n");
			pLine->close();
			return 0;
		}


		//===========加扩展数据===========
		pLine->upgradeOpen();
		if (pLine->setXData(xData) != Acad::eOk){

			ads_printf("\n警告: 实体(多义线 ID=%d)添加扩展数据时出错!\n",PlineId);

			pLine->close();
			return PlineId;
		}
		//================================

		pLine->close();
	}

⌨️ 快捷键说明

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