- UID
- 756
- 积分
- 197
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2002-1-16
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
[ 本帖最后由 Love-Lisp 于 2013-9-12 09:55 编辑 ]\n\n搜集的一些Object_Arx实用函数,初学者学习arx代码的好范例!- //添加Hatch
- AcDbObjectId CreateHatch(
- AcDbObjectId dbOId,
- char cLayer[],
- char cPattern[] = "SOLID",
- int nColor = 256,
- double dAngle = 0.0,
- double dScale = 1.0,
- AcDbDatabase * pDbDatab = acdbHostApplicationServices()->workingDatabase())
- {
- AcCmColor CmC;
- AcDbObjectId DbOId;
- AcDbObjectIdArray DbOIdA(0, 2);
- AcDbBlockTable * pDbBT;
- AcDbBlockTableRecord * pDbBTR;
- 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!
- pDbDatab->getSymbolTable(pDbBT, AcDb::kForRead);
- pDbBT->getAt(ACDB_MODEL_SPACE, pDbBTR, AcDb::kForWrite);
- pDbBTR->appendAcDbEntity(DbOId, pDbHat);
- pDbHat->setLayer(cLayer);
- CmC.setColorIndex(nColor);
- ((AcDbEntity *)pDbHat)->setColor(CmC);
- pDbBT->close();
- pDbBTR->close();
- pDbHat->close();
- return DbOId;
- }
- //************************************************************************
- //函数名称:drawDonutAtPoint
- //函数类型:void
- //返回值:
- //功能描述:
- //函数作者:Darcy
- //创建日期:2003-x-x
- //参数列表:
- //变量名:nPt 变量类型:const AcGePoint2d& 变量说明:中点
- //变量名:nRadius 变量类型:const double 变量说明:半径
- //************************************************************************
- void drawDonutAtPoint(const AcGePoint2d& nPt,const double nRadius)
- {
- AcGePoint2d pt;
- AcDbPolyline * pPline = new AcDbPolyline(2);
- pt = nPt;
- pt.x -= nRadius;
- pPline->addVertexAt( 0 , pt , 1.0 , nRadius*2.0 , nRadius*2.0);
- pt.x += nRadius * 2.0;
- pPline->addVertexAt( 1 , pt , 1.0 , nRadius*2.0 , nRadius*2.0);
- pPline->setClosed(Adesk::kTrue);
- //添加到当前数据库:
- addToCurrentSpaceAndClose(pPline);
- }
- 我也跟几贴,得点爱心币,互相交流
- //得到指定层上的所有实体
- Acad::ErrorStatus CDrawFunction::getAllEntity(AcDbDatabase *pDb,AcDbObjectIdArray& IdArr,
- const AcArray& layerNameArr)
- {
- Acad::ErrorStatus es=Acad::eOk;
- ASSERT(pDb);
- if(pDb==NULL)
- return Acad::eInvalidInput;
- AcDbBlockTable *pBlkTable=NULL;
- if((es=pDb->getBlockTable(pBlkTable,AcDb::kForRead))!=Acad::eOk)//打开块表
- {
- acedAlert("打开块表失败");
- return es;
- }
- AcDbBlockTableRecord *pBlkTableRecord=NULL;
- if((es=pBlkTable->getAt(ACDB_MODEL_SPACE,pBlkTableRecord,AcDb::kForRead))!=Acad::eOk)//打开块表记录
- {
- acedAlert("打开块表记录失败");
- pBlkTable->close();
- return es;
- }
- pBlkTable->close();//关闭块表
- AcDbBlockTableRecordIterator *pIterator=NULL; //创建叠代器
- if((es=pBlkTableRecord->newIterator(pIterator))!=Acad::eOk)
- {
- pBlkTableRecord->close();
- return es;
- }
- CAcModuleResourceOverride resoverride;
- CProgressDlg progress;
- progress.Create();
- progress.SetPos(0);
- progress.SetWindowText("正在检测图形中所有实体...");
- for(pIterator->start();!pIterator->done();pIterator->step())//遍历整个数据库
- {
- AcDbEntity *entity=NULL;
- es=pIterator->getEntity(entity,AcDb::kForRead); //打开实体
- if(es==Acad::eLockViolation)
- {
- acedAlert("内存锁定");
- }
- else if(es==Acad::eWasOpenForWrite)
- {
- acedAlert("实体以写方式打开");
- }
- else if(es==Acad::eWasOpenForRead)
- {
- acedAlert("实体以读方式打开");
- }
- else
- {
- if(layerNameArr.contains(entity->layer()))
- IdArr.append(entity->objectId());
- entity->close();
- }
- progress.StepIt();
- }
- delete pIterator;pIterator=NULL;
- pBlkTableRecord->close();
- acutPrintf("eend");
- return es;
- }
- //得到指定层上指定颜色的所有实体
- Acad::ErrorStatus CDrawFunction::getAllEntity(AcDbDatabase *pDb,AcDbObjectIdArray& IdArr,
- const AcArray& layerNameArr,Adesk::UInt16 colorIndex)
- {
- Acad::ErrorStatus es=Acad::eOk;
- ASSERT(pDb);
- if(pDb==NULL)
- return Acad::eInvalidInput;
- AcDbBlockTable *pBlkTable=NULL;
- if((es=pDb->getBlockTable(pBlkTable,AcDb::kForRead))!=Acad::eOk)//打开块表
- {
- acedAlert("打开块表失败");
- return es;
- }
- AcDbBlockTableRecord *pBlkTableRecord=NULL;
- if((es=pBlkTable->getAt(ACDB_MODEL_SPACE,pBlkTableRecord,AcDb::kForRead))!=Acad::eOk)//打开块表记录
- {
- acedAlert("打开块表记录失败");
- pBlkTable->close();
- return es;
- }
- pBlkTable->close();//关闭块表
- AcDbBlockTableRecordIterator *pIterator=NULL; //创建叠代器
- if((es=pBlkTableRecord->newIterator(pIterator))!=Acad::eOk)
- {
- pBlkTableRecord->close();
- return es;
- }
- CAcModuleResourceOverride resoverride;
- CProgressDlg progress;
- progress.Create();
- progress.SetPos(0);
- progress.SetWindowText("正在检测图形中所有实体...");
- for(pIterator->start();!pIterator->done();pIterator->step())//遍历整个数据库
- {
- AcDbEntity *entity=NULL;
- if((es=pIterator->getEntity(entity,AcDb::kForRead))!=Acad::eOk)//打开实体
- {
- delete pIterator;pIterator=NULL;
- pBlkTableRecord->close();
- return es;
- }
- if(layerNameArr.contains(entity->layer())&&entity->colorIndex()==colorIndex)
- IdArr.append(entity->objectId());
- entity->close();
- progress.StepIt();
- }
- delete pIterator;pIterator=NULL;
- pBlkTableRecord->close();
- return es;
- }
- //得到图形中所有的层
- Acad::ErrorStatus CDrawFunction::getAllLayerName(AcDbDatabase *pDb,CStringArray& layerArray)
- {
- Acad::ErrorStatus es=Acad::eOk;
- if(pDb==NULL)
- return Acad::eInvalidInput;
- layerArray.RemoveAll();
- AcDbLayerTable *pLayerTable=NULL;
- if((es=pDb->getSymbolTable(pLayerTable,AcDb::kForRead))!=Acad::eOk)
- {
- pLayerTable->close();
- return es;
- }
- //创建一个层表迭代器
- AcDbLayerTableIterator *pLayerTableIterator;
- pLayerTable->newIterator(pLayerTableIterator);
- pLayerTable->close();
- char *pLayerName=NULL;
- CString name;
- for(int i=0;!pLayerTableIterator->done();pLayerTableIterator->step(),i++)
- {
- AcDbLayerTableRecord *pLayerTableRecord=NULL;
- pLayerTableIterator->getRecord(pLayerTableRecord,AcDb::kForRead);
- pLayerTableRecord->getName(pLayerName);
- name.Format("%s",pLayerName);
- pLayerTableRecord->close();
- layerArray.Add(name);
- }
- if(pLayerName) acutDelString(pLayerName);
- delete pLayerTableIterator;pLayerTableIterator=NULL;
- return es;
- }
- //*************根据线形名得到线型ID********************//
- //******************************************************//
- BOOL CDrawFunction::getLinetypeIdFromString(const char* str, AcDbObjectId& id)
- {
- //----查找安装目录----//
- CString AcadInstallPath;
- FindAcadInstallPath(AcadInstallPath);
- CString File=AcadInstallPath+"\\linetype\\user.lin";
- //-----查找完毕--------//
- ASSERT(str!=NULL);
- AcDbLinetypeTable *pLinetypeTable;
- acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pLinetypeTable, AcDb::kForRead);
- Acad::ErrorStatus mess;
- mess=pLinetypeTable->getAt(str,id);
- if(mess==Acad::eKeyNotFound||mess==Acad::ePermanentlyErased)
- {
- pLinetypeTable->close();
- Acad::ErrorStatus error;
- error=acdbLoadLineTypeFile(str,File.GetBuffer(0),acdbHostApplicationServices()->workingDatabase());
- if(error==Acad::eNullObjectPointer)
- {
- AcDbLinetypeTable *pLinetypeTable;
- acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pLinetypeTable, AcDb::kForRead);
- pLinetypeTable->getAt("CONTINUOUS", id);
- pLinetypeTable->close();
- return FALSE;
- }
- else if(error==Acad::eFileSystemErr)
- {
- AfxMessageBox("the specified file cannot be opened");
- return FALSE;
- }
- else if(error==Acad::eUndefinedLineType)
- {
- AcDbLinetypeTable *pLinetypeTable;
- acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pLinetypeTable, AcDb::kForRead);
- AfxMessageBox("the linetype name specified by ltname is not found in the file");
- pLinetypeTable->getAt("CONTINUOUS", id);
- pLinetypeTable->close();
- return TRUE;
- }
- AcDbLinetypeTable *pLinetypeTable;
- acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pLinetypeTable, AcDb::kForRead);
- pLinetypeTable->getAt(str,id);
- pLinetypeTable->close();
- return TRUE;
- }
- pLinetypeTable->close();
- return TRUE;
- }
- void CDrawFunction::FindAcadInstallPath(CString &AcadInstallPath)
- {
- //查找样式目录安装路径
- TCHAR AcadPath[255];
- HKEY hKey;
- if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,TEXT("SOFTWARE\\线路处开发组\\RDS2002\\1.00"),0,KEY_QUERY_VALUE,&hKey)!=ERROR_SUCCESS)
- {
- ::AfxMessageBox("注册路径不对");
- return ;
- }
- DWORD dwDataType=REG_SZ;
- DWORD dwLength=255;
- LONG lRet=RegQueryValueEx(hKey,TEXT("path"),NULL,NULL,(LPBYTE)AcadPath,&dwLength);
- RegCloseKey(hKey);
- if(lRet!=ERROR_SUCCESS)
- {
- acutPrintf("Read failed\n");
- return ;
- }
- //-----查找完毕--------//
- AcadInstallPath.Format("%s",AcadPath);
- }
- AcDbObjectId CDrawFunction::createTextStyle(CString fontName,CString bigFontName,CString textStyleName)
- {
- AcGiTextStyle *TextStyle=new AcGiTextStyle
- (fontName,
- bigFontName,
- 0,
- 0.67,
- 0,
- 0,
- Adesk::kFalse,
- Adesk::kFalse,
- Adesk::kFalse,
- Adesk::kFalse,
- Adesk::kFalse,
- textStyleName); //字体名
- AcDbObjectId textStyleId;
- toAcDbTextStyle(*TextStyle,textStyleId);
- return textStyleId;
- }
- //*******************wuweifan2002.12.12*************************//
- //********************插入多行文本***************************//
- //************************************************************//
- AcDbObjectId CDrawFunction::createMutiText(AcGePoint3d BasePoint,AcDb::TextHorzMode hMode,AcDb::TextVertMode vMode,CString Text,double texthight,double widthfactor,double angle,int color,CString smallFontName,CString bigFontName,CString layerName)
- {
- ASSERT(Text!=NULL);
- AcDbMText *pMText=new AcDbMText();
- if(pMText==NULL)
- throw Acad::eOutOfMemory;
- AcDbObjectId TextStyleId;
- TextStyleId=createTextStyle(smallFontName,bigFontName,"xianlu");
- pMText->setTextStyle(TextStyleId);
- pMText->setContents(Text.GetBuffer(Text.GetLength()));
- pMText->setTextHeight(texthight);
- pMText->setRotation(angle);
- pMText->setLineSpacingFactor(0.8);
- pMText->setColorIndex(color);
- if(layerName!="")
- pMText->setLayer(layerName.GetBuffer(0));
- AcDbObjectId MTextId;
- addToModelSpace(MTextId, pMText);
- pMText->close();
- return MTextId;
- }
- //********************插入单行文本***************************//
- //************************************************************//
- AcDbObjectId CDrawFunction::createtextAll(AcGePoint3d pt,char *text,AcDb::TextHorzMode hMode,AcDb::TextVertMode vMode,double hight,double widthFactor,double rotation,int color,CString smallFontName,CString bigFontName,CString layerName)
- {
- ASSERT(text!=NULL);
- AcDbText *pText=NULL;
- pText=new AcDbText;
- if(pText==NULL)
- throw Acad::eOutOfMemory;
- AcDbObjectId textId;
- textId=createTextStyle(smallFontName,bigFontName,"xianlu");
- pText->setTextStyle(textId);
- pText->setTextString(text);
- pText->setHeight(hight);
- pText->setColorIndex(color);
- pText->setRotation(rotation);
- pText->setWidthFactor(widthFactor);
- pText->setPosition(pt);
- if(layerName!="")
- pText->setLayer(layerName.GetBuffer(0));
- addToModelSpace(textId, pText);
- pText->close();
- return textId;
- }
- //设置尺寸文本样式
- void CDrawFunction::setDimTextStyle(AcDbObjectId dimId,AcDbObjectId textStyleId,
- int colorIndex,double textHeight,double textScator,
- double textGap,bool align)
- {
- AcDbDimension *dimText;
- acdbOpenObject(dimText,dimId,AcDb::kForWrite);
- dimText->setDimtxsty(textStyleId); //文本字体DIMTXSTY
- AcCmColor color;
- color.setColorIndex(colorIndex);
- dimText->setDimclrt(color);//文本颜色DIMCLRT
- dimText->setDimtxt(textHeight); //文本高度DIMTXT
- dimText->setDimtfac(textScator);//文本高宽比DIMTFAC
- dimText->setDimgap(textGap);//文本距尺寸距离DIMGAP
- dimText->setDimtoh(align);//文本标注DIMTOH
- dimText->close();
- }
- //设置尺寸延伸线类型
- Acad::ErrorStatus CDrawFunction::setextensionlineStyle(AcDbObjectId dimId,int colorIndex,double length,
- double offLength,bool v1,bool v2)
- {
- Acad::ErrorStatus es=Acad::eOk;
- AcDbDimension *dimText=NULL;
- if((es=acdbOpenObject(dimText,dimId,AcDb::kForWrite))!=Acad::eOk)
- return es;
- AcCmColor color;
- if((es=color.setColorIndex(colorIndex))!=Acad::eOk)
- {
- dimText->close();return es;
- }
- dimText->setDimclre(color);//设置颜色DIMCLRE
- dimText->setDimexe(length);//设置超出长度DIMEXE
- dimText->setDimexo(offLength);//尺寸偏离长度DIMEXO
- dimText->setDimse1(v1);//是否注第一条线DIMSE1
- dimText->setDimse2(v2);//是否注第二条线DIMSE2
- dimText->close();
- return es;
- }
- //绘制对齐尺寸线
- AcDbObjectId CDrawFunction::drawDimension(AcGePoint3d xLine1Point,AcGePoint3d xLine2Point,
- double fwj,int direction,double distance,CString dimText,CString m_cLayerName)
- {
- AcDbAlignedDimension *dimension=new AcDbAlignedDimension;
- AcGePoint3d dimLinePoint;
- // CCalcuMethod *calcu=new CCalcuMethod();
- // calcu->calEndZbSelf(xLine2Point,distance*direction,fwj,dimLinePoint);
- // delete calcu;calcu=NULL;
- dimension->setXLine1Point(xLine1Point);
- dimension->setDimLinePoint(dimLinePoint);
- dimension->setXLine2Point(xLine2Point);
- dimension->setDimensionText(dimText.GetBuffer(0));
- dimension->setLayer(m_cLayerName.GetBuffer(0));
- AcDbObjectId dimId;
- addToModelSpace(dimId,dimension);
- dimension->close();
- return dimId;
- }
- Acad::ErrorStatus CDrawFunction::createLine(AcDbObjectId &lineId,AcGePoint3d startPt,AcGePoint3d endPt,int color,CString Layer,char *linetype)
- {
- Acad::ErrorStatus es=Acad::eOk;
- ASSERT(linetype!=NULL);
- AcDbLine *pLine = new AcDbLine(startPt, endPt);
- if((es=pLine->setColorIndex(color))!=Acad::eOk)
- {
- pLine->close();return es;
- }
- if(Layer!="")
- {
- if(pLine->setLayer(Layer)==Acad::eKeyNotFound \
- ||pLine->setLayer(Layer)==Acad::eDeletedEntry )
- {
- createNewLayer(Layer);
- if((es=pLine->setLayer(Layer.GetBuffer(0)))!=Acad::eOk)
- {
- pLine->close();return es;
- }
- }
- }
- if(linetype!=NULL)
- {
- 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;
- }
- }
- }
- es=addToModelSpace(lineId,pLine);
- return es;
- }
- Acad::ErrorStatus CDrawFunction::createCircle(AcDbObjectId& circleId,AcGePoint3d center,double radius,int color,CString layer)
- {
- Acad::ErrorStatus es=Acad::eOk;
- AcGeVector3d normal(0,0,1);
- AcDbCircle *circle=new AcDbCircle(center,normal,radius);
- if((es=circle->setColorIndex(color))!=Acad::eOk)
- {
- circle->close();return es;
- }
- if(layer!="")
- {
- if(circle->setLayer(layer)==Acad::eKeyNotFound \
- ||circle->setLayer(layer)==Acad::eDeletedEntry )
- {
- createNewLayer(layer);
- if((es=circle->setLayer(layer.GetBuffer(0)))!=Acad::eOk)
- {
- circle->close();return es;
- }
- }
- }
- es=addToModelSpace(circleId,circle);
- return es;
- }
- Acad::ErrorStatus CDrawFunction::DrawPolyline(AcDbObjectId& polylineId, AcGePoint3dArray ptArr, int Color, double Width,bool IsClose,CString Layer,char *linetype)
- {
- Acad::ErrorStatus es=Acad::eOk;
- AcDb2dPolyline *pNewPline;
- if(IsClose)pNewPline=new AcDb2dPolyline(AcDb::k2dSimplePoly,ptArr,0,Adesk::kTrue,Width,Width);
- else pNewPline=new AcDb2dPolyline(AcDb::k2dSimplePoly,ptArr,0,Adesk::kFalse,Width,Width);
- if((es=pNewPline->setColorIndex(Color))!=Acad::eOk)
- {
- pNewPline->close();return es;
- }
- if(Layer!="")
- {
- if(pNewPline->setLayer(Layer)==Acad::eKeyNotFound \
- ||pNewPline->setLayer(Layer)==Acad::eDeletedEntry )
- {
- createNewLayer(Layer);
- if((es=pNewPline->setLayer(Layer))!=Acad::eOk)
- {
- pNewPline->close();return es;
- }
- }
- }
- if(linetype!=NULL)
- {
- 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;
- }
- }
- es=addToModelSpace(polylineId,pNewPline);
- return es;
- }
- Acad::ErrorStatus CDrawFunction::DrawSplinePolyline(AcDbObjectId& polylineId, AcGePoint3dArray ptArr, int Color, double Width,bool IsClose,CString Layer,char *linetype)
- {
- Acad::ErrorStatus es=Acad::eOk;
- AcDb2dPolyline *pNewPline;
- 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!="")
- {
- if(pNewPline->setLayer(Layer)==Acad::eKeyNotFound \
- ||pNewPline->setLayer(Layer)==Acad::eDeletedEntry )
- {
- createNewLayer(Layer);
- if((es=pNewPline->setLayer(Layer))!=Acad::eOk)
- {
- pNewPline->close();return es;
- }
- }
- }
- if(linetype!=NULL)
- {
- 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;
- }
- }
- es=addToModelSpace(polylineId,pNewPline);
- return es;
- }
- //得到文本边界
- void CDrawFunction::getTextBoundary(AcDbObjectId objectId,double offset,AcDbObjectId &textBoundaryId)
- {
- AcDbExtents Ext;
- AcDbEntity *pEnt;
- acdbOpenObject(pEnt,objectId,AcDb::kForWrite);
- if(pEnt->isKindOf(AcDbText::desc()))
- {
- AcDbText *pText=AcDbText::cast(pEnt);
- AcGePoint3d basePoint;
- basePoint=pText->position();
- double rotateAngle=pText->rotation();
- pText->setRotation(0);
- pText->getGeomExtents(Ext);
- AcGePoint3d minPt,maxPt;
- minPt=Ext.minPoint();
- maxPt=Ext.maxPoint();
- AcGePoint3dArray pointArray;
- AcGePoint3d point1,point2,point3,point4;
- point1.x=minPt.x-offset;point1.y=minPt.y-offset;point1.z=0;
- pointArray.append(point1);
- point2.x=maxPt.x+offset;point2.y=minPt.y-offset;point2.z=0;
- pointArray.append(point2);
- point3.x=maxPt.x+offset;point3.y=maxPt.y+offset;point3.z=0;
- pointArray.append(point3);
- point4.x=minPt.x-offset;point4.y=maxPt.y+offset;point4.z=0;
- pointArray.append(point4);
- DrawPolyline(textBoundaryId,pointArray,1,0,TRUE,"0","CONTINUOUS");
- AcGeMatrix3d matrix;
- AcGeVector3d axis;
- ident_init(matrix);
- axis.set(0,0,1);
- matrix=matrix.rotation(rotateAngle,axis,basePoint); //旋转矩阵
- AcDbEntity *BounEnt;
- acdbOpenObject(BounEnt,textBoundaryId,AcDb::kForWrite);
- BounEnt->transformBy(matrix);
- BounEnt->close();
- pText->setRotation(rotateAngle);
- }
- else if(pEnt->isKindOf(AcDbMText::desc()))
- {
- AcDbMText *pMtext=AcDbMText::cast(pEnt);
- AcGePoint3d basePoint;
- basePoint=pMtext->location();
- double rotateAngle=pMtext->rotation();
- pMtext->setRotation(0);
- AcGePoint3dArray pointArray;
- double width=pMtext->actualWidth();
- double height=pMtext->actualHeight();
- AcGePoint3d point1,point2,point3,point4;
- point1.x=basePoint.x-offset;point1.y=basePoint.y+offset;point1.z=0;
- pointArray.append(point1);
- point2.x=basePoint.x+width+offset;point2.y=basePoint.y+offset;point2.z=0;
- pointArray.append(point2);
- point3.x=basePoint.x+width+offset;point3.y=basePoint.y-height-offset;point3.z=0;
- pointArray.append(point3);
- point4.x=basePoint.x-offset;point4.y=basePoint.y-height-offset;point4.z=0;
- pointArray.append(point4);
- DrawPolyline(textBoundaryId,pointArray,1,0,TRUE,"0","CONTINUOUS");
- AcGeMatrix3d matrix;
- AcGeVector3d axis;
- ident_init(matrix);
- axis.set(0,0,1);
- matrix=matrix.rotation(rotateAngle,axis,basePoint); //旋转矩阵
- AcDbEntity *BounEnt;
- acdbOpenObject(BounEnt,textBoundaryId,AcDb::kForWrite);
- BounEnt->transformBy(matrix);
- BounEnt->close();
- pMtext->setRotation(rotateAngle);
- }
- pEnt->close();
- return;
- }
- AcDbObjectId CDrawFunction::createNewLayer(CString LayerName)
- {
- AcDbLayerTable *LayerTable;
- acdbHostApplicationServices()->workingDatabase()->getSymbolTable(LayerTable,AcDb::kForWrite);
- AcDbObjectId LayerId;
- if(!LayerTable->has(LayerName))
- {
- AcDbLayerTableRecord *LayerTableRecord=new AcDbLayerTableRecord;
- LayerTableRecord->setName(LayerName);
- LayerTable->add(LayerId,LayerTableRecord);
- LayerTableRecord->close();
- }
- else
- {
- LayerTable->getAt(LayerName,LayerId,FALSE);
- }
- LayerTable->close();
- return LayerId;
- }
- bool CDrawFunction::insertBlock(AcDbObjectId &newEntId,CString BlockName,double fwj,AcGePoint3d basePoint,double scalex,
- CString Text1,CString Text2,CString Text3,
- int text1color,int text2color,int text3color,
- double text1height,double text2height,double text3height,
- double text1scator,double text2scator,double text3scator,
- CString littleFont,CString bigFont,CString layerName,double dx,double dy) //每块可有三个属性定义
- {
- AcDbObjectId blockId;
- double x,y;
- x=basePoint[X];
- y=basePoint[Y];
- bool a=AcDbSymbolUtilities::hasBlock(BlockName.GetBuffer(BlockName.GetLength()),acdbHostApplicationServices()->workingDatabase());
- if(!a)return FALSE;
- AcDbSymbolUtilities::getBlockId(blockId,BlockName.GetBuffer(BlockName.GetLength()),acdbHostApplicationServices()->workingDatabase());
- AcDbBlockReference *pBlkRef = new AcDbBlockReference;
- // pBlkRef->treatAsAcDbBlockRefForExplode();///////////
- pBlkRef->setBlockTableRecord(blockId);
- pBlkRef->setLayer(layerName);//设置层/////////////////////////////
- struct resbuf to, from;
- from.restype = RTSHORT;
- from.resval.rint = 1; // UCS
- to.restype = RTSHORT;
- to.resval.rint = 0; // WCS
- AcGeVector3d normal(0, 0, 1);
- acedTrans(&(normal.x), &from, &to, Adesk::kTrue,&(normal.x));
- AcGeScale3d scale(scalex,scalex,scalex);
- pBlkRef->setScaleFactors(scale);
- AcGePoint3d insertPoint;
- insertPoint=basePoint;
- pBlkRef->setPosition(basePoint);
- pBlkRef->setRotation(fwj);
- pBlkRef->setNormal(normal);
- pBlkRef->setColorIndex(text1color);//按文本颜色设置改变颜色
- pBlkRef->setLayer(layerName);
- AcDbBlockTable *pBlockTable;
- acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlockTable,AcDb::kForWrite);
- AcDbBlockTableRecord *pBlockTableRecord;
- pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord,AcDb::kForWrite);
- pBlockTableRecord->appendAcDbEntity(newEntId, pBlkRef);
- pBlockTable->close();
- pBlockTableRecord->close();
- acdbOpenObject(pBlockTableRecord, blockId, AcDb::kForRead);
- AcDbBlockTableRecordIterator *pIterator;
- pBlockTableRecord->newIterator(pIterator);
- AcDbEntity *pEnt;
- AcDbAttributeDefinition *pAttdef;
- for (pIterator->start(); !pIterator->done();pIterator->step())
- {
- pIterator->getEntity(pEnt, AcDb::kForWrite);
- //pEnt->setColorIndex(text1color);//按文本颜色设置改变颜色
- //pEnt->setLayer(layerName);//设置层
- if(pEnt->isKindOf(AcDbAttributeDefinition::desc()))
- {
- pAttdef = AcDbAttributeDefinition::cast(pEnt);
- if (pAttdef != NULL && !pAttdef->isConstant())
- {
- AcDbAttribute *pAtt = new AcDbAttribute();
- pAtt->setPropertiesFrom(pAttdef);
- pAtt->setInvisible(pAttdef->isInvisible());
- AcGePoint3d alignpoint;
- alignpoint=pAttdef->alignmentPoint();
- alignpoint+=pBlkRef->position().asVector();
- AcDbObjectId TextStyleId;
- TextStyleId=createTextStyle(littleFont,bigFont,"xianlu");
- pAtt->setTextStyle(TextStyleId);
- pAtt->setHeight(pAttdef->height());
- pAtt->setTag("Tag");
- pAtt->setFieldLength(25);
- pAtt->setLayer(layerName);
- char *pStr = pAttdef->tag();
- pAtt->setTag(pStr);
- free(pStr);
- pAtt->setFieldLength(pAttdef->fieldLength());
- if(strcmp(pAtt->tag(),"标注1")==0)
- {
- AcGePoint3d gg(dx/scalex,0,0);
- alignpoint=alignpoint+gg.asVector();
- AcGeVector3d vet(0,0,1);
- alignpoint=alignpoint.scaleBy(scalex,insertPoint);
- alignpoint=alignpoint.rotateBy(fwj,vet,insertPoint);
- pAtt->setTextString(Text1.GetBuffer(Text2.GetLength()));
- pAtt->setRotation(PI/2+fwj);
- pAtt->setHorizontalMode(AcDb::kTextCenter);
- pAtt->setVerticalMode(AcDb::kTextBottom);
- pAtt->setAlignmentPoint(alignpoint);
- pAtt->setColorIndex(text1color);
- pAtt->setHeight(text1height*scalex);
- pAtt->setWidthFactor(text1scator);
- }
- if(strcmp(pAtt->tag(),"标注2")==0)
- {
- AcGePoint3d gg(dx/scalex,0,0);
- alignpoint=alignpoint-gg.asVector();
- AcGeVector3d vet(0,0,1);
- alignpoint=alignpoint.scaleBy(scalex,insertPoint);
- alignpoint=alignpoint.rotateBy(fwj,vet,insertPoint);
- pAtt->setTextString(Text2.GetBuffer(Text2.GetLength()));
- pAtt->setRotation(PI/2+fwj);
- pAtt->setHorizontalMode(AcDb::kTextCenter);
- pAtt->setVerticalMode(AcDb::kTextBottom);
- pAtt->setAlignmentPoint(alignpoint);
- pAtt->setColorIndex(text2color);
- pAtt->setHeight(text2height*scalex);
- pAtt->setWidthFactor(text2scator);
- }
- if(strcmp(pAtt->tag(),"标注3")==0)
- {
- AcGePoint3d gg(0,dy/scalex,0);
- alignpoint=alignpoint+gg.asVector();
- AcGeVector3d vet(0,0,1);
- alignpoint=alignpoint.scaleBy(scalex,insertPoint);
- alignpoint=alignpoint.rotateBy(fwj,vet,insertPoint);
- pAtt->setTextString(Text3.GetBuffer(Text3.GetLength()));
- pAtt->setRotation(fwj);
- pAtt->setHorizontalMode(AcDb::kTextCenter);
- pAtt->setVerticalMode(AcDb::kTextBottom);
- pAtt->setAlignmentPoint(alignpoint);
- pAtt->setColorIndex(text3color);
- pAtt->setHeight(text3height*scalex);
- pAtt->setWidthFactor(text3scator);
- }
- AcDbObjectId attId;
- pBlkRef->appendAttribute(attId, pAtt);
- pAtt->close();
- }
- }
- // pEnt->setColorIndex(text1color);//按文本颜色设置改变颜色
- pEnt->close();
- }
- delete pIterator;
- pBlockTableRecord->close();
- pBlkRef->close();
- return TRUE;
- }
- //遍历多义线顶点坐标
- Acad::ErrorStatus CDrawFunction::IteratorPolyline(AcDbObjectId polylineID,AcGePoint3dArray& pointArray)
- {
- Acad::ErrorStatus es=Acad::eOk;
- AcDbObject* pObject=NULL;
- AcDbPolyline *pline=NULL;
- if((es=acdbOpenObject(pObject,polylineID,AcDb::kForRead))!=Acad::eOk)
- return es;
- if(!pObject->isKindOf(AcDbPolyline::desc()))
- {
- pObject->close();
- return Acad::eInvalidInput;
- }
- pline=AcDbPolyline::cast(pObject);
- int num=pline->numVerts();
- for (int i=0; i< num; i++)
- {
- AcGePoint3d temPt;
- if((es=pline->getPointAt(i, temPt))!=Acad::eOk)
- {
- pObject->close();
- return Acad::eInvalidInput;
- }
- pointArray.append(temPt);
- }
- pObject->close();
- return es;
- }
- Acad::ErrorStatus CDrawFunction::createGroup(CString groupname,AcDbObjectIdArray IdArray)
- {
- Acad::ErrorStatus es=Acad::eOk;
- AcDbDictionary *pGroupDict=NULL;
- AcDbGroup *pGroup=NULL;
- if((es=acdbHostApplicationServices()->workingDatabase()->getGroupDictionary(pGroupDict,AcDb::kForWrite))!=Acad::eOk)
- {
- return es;
- }
- AcDbObjectId groupId;
- es=pGroupDict->getAt(groupname,groupId);
- if(es==Acad::eInvalidKey)
- {
- acutPrintf("\n输入的词典名无效!");
- pGroupDict->close();
- return es;
- }
- else if(es==Acad::eKeyNotFound)
- {
- pGroup=new AcDbGroup("GroupDiscription");
- if((es=pGroupDict->setAt(groupname,pGroup,groupId))!=Acad::eOk)
- {
- pGroup->close();pGroupDict->close();return es;
- }
- }
- else if(es==Acad::eOk )
- {
- if((es=acdbOpenObject(pGroup,groupId,AcDb::kForWrite))!=Acad::eOk)
- {
- pGroupDict->close();return es;
- }
- }
- for(int i=0;i<IDARRAY.LENGTH();I++)
- pGroup->append(IdArray[i]);
- pGroup->setSelectable(FALSE);
- pGroupDict->close();
- pGroup->close();
- return es;
- }
- double CDrawFunction::getTextLength(AcDbObjectId textId)
- {
- Acad::ErrorStatus es=Acad::eOk;
- AcDbEntity *pEnt=NULL;
- if((es=acdbOpenObject(pEnt,textId,AcDb::kForRead))!=Acad::eOk)
- return -1;
- AcDbExtents Ext;
- pEnt->getGeomExtents(Ext);
- pEnt->close();
- AcGePoint3d minPt,maxPt;
- minPt=Ext.minPoint();
- maxPt=Ext.maxPoint();
- return acutDistance(asDblArray(minPt),asDblArray(maxPt));
- }
- void CDrawFunction::highlightEdge(const AcDbObjectId& objId,const int marker)
- {
- char dummy[133];
- AcDbEntity *pEnt;
- acdbOpenObject(pEnt,objId,AcDb::kForRead);
- AcGePoint3d pickpnt;
- AcGeMatrix3d xform;
- int numIds;
- AcDbFullSubentPath *subentIds;
- pEnt->getSubentPathsAtGsMarker(AcDb::kEdgeSubentType,marker,pickpnt,xform,numIds,subentIds);
- if(numIds>0)
- {
- pEnt->highlight(subentIds[0]);
- ads_getstring(0,"\npressto continue...",dummy);
- pEnt->unhighlight(subentIds[0]);
- }
- delete []subentIds;
- pEnt->close();
- }
- Acad::ErrorStatus CDrawFunction::readXrecord(CString dictName,CString xrecordName,CString &message)
- {
- AcDbDictionary *pDict=NULL;
- pDict=openDictionaryForRead(dictName,acdbHostApplicationServices()->workingDatabase());
- if(pDict)
- {
- AcDbXrecord *pXrec;
- pDict->getAt(xrecordName, (AcDbObject*&) pXrec,AcDb::kForRead);
- pDict->close();
- struct resbuf *pRbList;
- pXrec->rbChain(&pRbList);
- pXrec->close();
- message=pRbList->resval.rstring;
- acutRelRb(pRbList);
- return Acad::eOk;
- }
- else
- {
- return Acad::eInvalidInput;
- }
- }
- //**********************扩展记录操作(2002.12.12)***************//
- //****************打开扩展记录为写******************************//
- //**************************************************************//
- AcDbDictionary* CDrawFunction::openDictionaryForWrite(LPCTSTR dictName,
- bool createIfNotFound,AcDbDictionary* parentDict)
- {
- ASSERT(dictName != NULL);
- ASSERT(parentDict != NULL);
- ASSERT(parentDict->isWriteEnabled());
- AcDbDictionary* dict = NULL;
- AcDbObject* obj;
- Acad::ErrorStatus es;
- es = parentDict->getAt(dictName, obj, AcDb::kForWrite);
- if (es == Acad::eOk)
- {
- dict = AcDbDictionary::cast(obj);
- }
- else if (es == Acad::eKeyNotFound)
- {
- if (createIfNotFound)
- {
- dict = new AcDbDictionary;
- AcDbObjectId dictId;
- es = parentDict->setAt(dictName, dict, dictId);
- if (es != Acad::eOk)
- {
- delete dict;dict = NULL;
- }
- }
- }
- return dict;
- }
- /******************************************************************************/
- /* 関数名 : SetOSMODE */
- /* 機能 : 設置OSMODE */
- /* 入力引数 : int OSMODE値 */
- /* 出力引数 : 無 */
- /* 返り値 : 無 */
- /* 備考 : 無 */
- /******************************************************************************/
- void SetOSMODE(int nOSMODE)
- {
- struct resbuf rebNewOSMODE;
- rebNewOSMODE.restype = RTSHORT;
- rebNewOSMODE.resval.rint = nOSMODE;
- /*設置系統變量OSNAP的値。*/
- acedSetVar("OSMODE",&rebNewOSMODE);
- }
- /******************************************************************************/
- /* 関数名 : GetCrossPoint */
- /* 機能 : 得到交叉點的位置和交點坐標。 */
- /* 入力引数 : AcGePoint3dArray Polyline的點列。 */
- /* AcGePoint3d 選擇点。 */
- /* double PickBox的長度。 */
- /* Adesk::Boolean 閉合屬性。 */
- /* 出力引数 : int 交叉點的位置(前)。 */
- /* int 交叉點的位置(後)。 */
- /* AcGePoint3d 交點坐標 */
- /* 返り値 : TRUE 正常。 */
- /* FALSE 異常。 */
- /* 備考 : 無 */
- /******************************************************************************/
- BOOL GetCrossPoint(AcGePoint3dArray gePoi3dA, AcGePoint3d gePoi3d, double dPickLen, int & iCrsID1,
- int & iCrsID2, AcGePoint3d & gePoi3dCrs, Adesk::Boolean bIsClosed = Adesk::kTrue)
- {
- ads_point spt1, spt2, spt3, spt4;
- AcGePoint3d GePoi3dStart, GePoi3dNext;
- int iReturn, iNum;
- iNum = gePoi3dA.length();
- if (iNum < 2) {
- return FALSE;
- }
- /*得到交叉點的位置*/
- for (int i = 0; i < iNum; i++) {
- iCrsID1 = i;
- //得到前一個點
- asPnt3d(spt3) = gePoi3dA[iCrsID1];
- //得到後一個點
- if (i == iNum - 1) {
- //實體是否是閉合。
- if (!bIsClosed) {
- break;
- }
- else {
- iCrsID2 = 0;
- }
- }
- else {
- iCrsID2 = i + 1;
- }
- asPnt3d(spt4) = gePoi3dA[iCrsID2];
- spt3[Z] = 0;
- spt4[Z] = 0;
- /*X方向是否有交叉點。*/
- spt1[X] = gePoi3d.x + (dPickLen / 2); spt2[X] = gePoi3d.x - (dPickLen / 2);
- spt1[Y] = gePoi3d.y; spt2[Y] = gePoi3d.y;
- spt1[Z] = 0; spt2[Z] = 0;
- iReturn = acdbInters(spt1, spt2, spt3, spt4, 1, asDblArray(gePoi3dCrs));
- if (iReturn == RTNORM) {
- return TRUE;
- }
- /*Y方向是否有交叉點。*/
- spt1[X] = gePoi3d.x; spt2[X] = gePoi3d.x;
- spt1[Y] = gePoi3d.y + (dPickLen / 2); spt2[Y] = gePoi3d.y - (dPickLen / 2);
- spt1[Z] = 0; spt2[Z] = 0;
- iReturn = acdbInters(spt1, spt2, spt3, spt4, 1, asDblArray(gePoi3dCrs));
- if (iReturn == RTNORM) {
- return TRUE;
- }
- }
- return FALSE;
- }
- 前阵困扰我的问题解决了,现在共享给大家:
- //************************************************************************
- //函数名称:getPointAtDistInGeCurve
- //函数类型:Acad::ErrorStatus
- //返回值:
- //功能描述:返回曲线上距起点某距离值处的点。
- //函数作者:Darcy
- //创建日期:2003-XX-XX
- //参数列表:
- //变量名:pGeCurve 变量类型:const AcGeCurve3d * 变量说明:
- //变量名:dist 变量类型:double 变量说明:
- //变量名:point 变量类型:AcGePoint3d& 变量说明:
- //备 注:
- //************************************************************************
- Acad::ErrorStatus getPointAtDistInGeCurve(const AcGeCurve3d * pGeCurve,double dist,AcGePoint3d& point)
- {
- Acad::ErrorStatus es = Acad::eOk;
- if ( pGeCurve != NULL )
- {
- AcGePoint3d spt;
- double pa=0.,datumParam=0.;
- //距离从起点起算!
- Adesk::Boolean posParamDir=Adesk::kTrue;
- pGeCurve->hasStartPoint(spt);
- datumParam = pGeCurve->paramOf(spt);;
- pa = pGeCurve->paramAtLength(
- datumParam,
- dist,
- posParamDir
- );
- point=pGeCurve->evalPoint(pa);
- }
- else
- es = Acad::eInvalidInput;
- return es;
- }
- bool
- IsAtLine(CAD_POINT StartPt,CAD_POINT EndPt,CAD_POINT thePt)
- /* 判断某点是否在直线上
- StartPt 直线起点
- EndPt 直线终点
- thePt 所判断点
- 返回:TRUE__在直线上 FALSE__不在直线上
- */
- {
- //为保证计算精度,首先对点的坐标数据降级
- CAD_VECTOR vec(StartPt.x,StartPt.y,StartPt.z);
- StartPt=StartPt-vec;
- EndPt=EndPt-vec;
- thePt=thePt-vec;
- // 直线方程系数
- double A,B,C;
- A = EndPt.y - StartPt.y;
- B = StartPt.x - EndPt.x;
- C = -1.0*(A*StartPt.x+B*StartPt.y);
- if(StartPt.x==EndPt.x)
- {
- if(thePt.y>max(StartPt.y,EndPt.y) || thePt.y<MIN(STARTPT.Y,ENDPT.Y))
- return FALSE;
- }
- else if(StartPt.y==EndPt.y)
- {
- if(thePt.x>max(StartPt.x,EndPt.x) || thePt.x<MIN(STARTPT.X,ENDPT.X))
- return FALSE;
- }
- else
- {
- if( thePt.x>max(StartPt.x,EndPt.x) || thePt.x
- thePt.y>max(StartPt.y,EndPt.y) || thePt.y
- return FALSE;
- }
- if(fabs(A*thePt.x+B*thePt.y+C)>1.0E-6)
- {
- return FALSE;
- }
- return TRUE;
- }
- /*****************************************************/
- BOOL
- IsAtArc(CAD_POINT firstPt,CAD_POINT secondPt,
- double radius,double direct,int More,CAD_POINT thePt)
- /* 判断某点是否在圆弧上
- firstPt 圆弧起点
- secondPt 圆弧终点
- radius 半径
- direct 偏向( >=0__左偏 <0__右偏 )
- More (More<0__小圆弧,More>0__大圆弧)
- thePt 判断点
- 返回:TRUE__在圆弧上 FALSE__不在圆弧上
- */
- {
- CAD_POINT centerPt,sectionPt;
- CAD_POINT startPt,endPt;
- double startAngle,endAngle,chordAngle,vertAngle;
- double intLine,chordLine;
- /* centerPt 圆弧圆心
- sectionPt 弦线中心点
- startAngle 圆弧起点切线角度(弧度)
- endAngle 圆弧终点切线角度(弧度)
- chordAngle 弦线角度(弧度)
- vertAngle 与弦线垂直的垂线角度(弧度)
- intLine 弦线中心至圆心距离
- chordLine 弦线长度
- */
- sectionPt.x = (firstPt.x + secondPt.x)/2;
- sectionPt.y = (firstPt.y + secondPt.y)/2;
- chordLine = sqrt( pow( (secondPt.x-firstPt.x),2 ) + pow( (secondPt.y-firstPt.y),2 ) );
- intLine = sqrt((radius*radius-chordLine*chordLine/4) );
- chordAngle = ads_angle(asDblArray(firstPt),asDblArray(secondPt)); //弦线的角度
- if(direct>=0)//左偏
- {
- startPt=firstPt;
- endPt=secondPt;
- vertAngle=chordAngle+Pai/2;
- }
- else if(direct<0)//右偏
- {
- startPt=secondPt;
- endPt=firstPt;
- vertAngle=chordAngle-Pai/2;
- }
- if(More<=0)//小圆弧
- {
- centerPt.x=sectionPt.x+intLine*cos(vertAngle);
- centerPt.y=sectionPt.y+intLine*sin(vertAngle);
- }
- else//大圆弧
- {
- centerPt.x=sectionPt.x-intLine*cos(vertAngle);
- centerPt.y=sectionPt.y-intLine*sin(vertAngle);
- }
- if(fabs(centerPt.distanceTo(thePt)-radius)>1.0E-8)
- return FALSE;
- startAngle = ads_angle(asDblArray(centerPt),asDblArray(startPt));
- endAngle = ads_angle(asDblArray(centerPt),asDblArray(endPt));
- AcDbArc *pArc=new AcDbArc(centerPt,radius,startAngle,endAngle);
- AcDbLine *pLine=new AcDbLine(centerPt,thePt);
- AcGePoint3dArray Points;
- pLine->intersectWith(pArc,AcDb::kOnBothOperands,Points);
- if(Points.isEmpty()) return FALSE;
- return TRUE;
- }
- 有时候,你需要这样调用Autocad命令
- void SendCommandToAutoCad(CString cmd)
- {
- COPYDATASTRUCT cmdMsg;
- cmdMsg.dwData = (DWORD)1;
- cmdMsg.cbData = (DWORD)_tcslen(cmd) + 1;
- cmdMsg.lpData = cmd.GetBuffer(cmd.GetLength()+1) ;
- SendMessage(acedGetAcadFrame()->m_hWnd, WM_COPYDATA, NULL, (LPARAM)&cmdMsg);
- }
- 向AcDbObject添加扩展数据Xdata
- 代码:
- void affixXdata(char *appName, char *xData, AcDbObject *pObj)
- {
- //向AcDbObject添加扩展数据Xdata
- struct resbuf *pRb, *pTemp;
- acdbRegApp(appName);
- pRb = acutNewRb(AcDb::kDxfRegAppName);
- pTemp = pRb;
- pTemp->resval.rstring = new char[strlen(appName)+1];
- strcpy(pTemp->resval.rstring, appName);
- pTemp->rbnext = acutNewRb(AcDb::kDxfXdAsciiString);
- pTemp = pTemp->rbnext;
- pTemp->resval.rstring = new char[strlen(xData)+1];
- strcpy(pTemp->resval.rstring, xData);
- pObj->setXData(pRb);
- acutRelRb(pRb);
- }
- //添加扩展数据
- //实体添加扩展数据(字符串)
- bool AddXData(CString appName, AcDbObjectId entId,CString data)
- {
- //open entity for read
- AcDbEntity*pEnt;
- Acad::ErrorStatus es=acdbOpenAcDbEntity(pEnt,entId,AcDb::kForRead);
- if(es!=Acad::eOk)
- {
- ads_printf("error in open entity\n");
- return false;
- }
- //get XData buffer
- struct resbuf*pRb,*pTemp;
- pRb=pEnt->xData(appName);
- if(pRb!=NULL)//have XData
- {
- //pTemp移到表尾
- pTemp=pRb;
- for(pTemp=pRb;pTemp->rbnext!=NULL;pTemp=pTemp->rbnext){;}
- }
- else//NOT have XData
- {
- //create new xData
- ads_regapp(appName);
- pRb=ads_newrb(AcDb::kDxfRegAppName);
- pRb->resval.rstring=(char*)malloc(appName.GetLength()+1);
- strcpy(pRb->resval.rstring,appName);
- pTemp=pRb;
- }
- //fill xData string
- pTemp->rbnext=ads_newrb(AcDb::kDxfXdAsciiString);
- pTemp=pTemp->rbnext;
- pTemp->resval.rstring=(char*)malloc(data.GetLength()+1);
- strcpy(pTemp->resval.rstring,data);
- //add xData
- es=pEnt->upgradeOpen();
- if(es!=Acad::eOk)
- {
- ads_printf("\nError occur in updateOpen.");
- pEnt->close();
- ads_relrb(pRb);
- return false;
- }
- es=pEnt->setXData(pRb);
- if(es!=Acad::eOk)
- {
- ads_printf("\nError occur in setXData.");
- pEnt->close();
- ads_relrb(pRb);
- return false;
- }
- //
- pEnt->close();
- ads_relrb(pRb);
- return true;
- }
- //发命令前加按了两个ESCAPE
- void SendCommand(char *cmd)
- {
- HWND wnd;
- char cp[3];
- wnd = adsw_acadMainWnd();
- if(!wnd) return;
- COPYDATASTRUCT cmddata;
- cp[0] = VK_ESCAPE;
- cp[1] = VK_ESCAPE;
- cp[2] = NULL;
- cmddata.dwData = (DWORD)1;
- cmddata.cbData = (DWORD)strlen(cp)+1;
- cmddata.lpData = cp;
- SendMessage(wnd,WM_COPYDATA,(WPARAM)cp,(LPARAM)&cmddata);
- cmddata.dwData = (DWORD)1;
- cmddata.cbData = (DWORD)strlen(cmd)+1;
- cmddata.lpData = cmd;
- SendMessage(wnd,WM_COPYDATA,(WPARAM)wnd,(LPARAM)&cmddata);
- }
- //函数功能:根据用户指定的两点,自动创建破断线
- void CAD_EXTBreakLine()
- {
- acutPrintf("指定两点,自动创建折线破断线\n");
- ads_point StartPoint,EndPoint;
- if(acedGetPoint(NULL,"\n请指定破断线的起点:",StartPoint)!=RTNORM) return;
- if(acedGetPoint(StartPoint,"\n请指定破断线的终点:",EndPoint)!=RTNORM) return;
- AcGePoint3d Start,End;
- End = AcGePoint3d(EndPoint[X],EndPoint[Y],0);
- Start = AcGePoint3d(StartPoint[X],StartPoint[Y],0);
- float Length = Start.distanceTo(End);
- AcGeVector3d Normal = End-Start;
- Normal = Normal.normal(AcGeContext::gTol);
- AcGePoint3d Point1(Start-Length*Normal*0.15);
- AcGePoint3d Point2(Start+Length*Normal*0.45);
- AcGePoint3d Point5(End-Length*Normal*0.45);
- AcGePoint3d Point6(End+Length*Normal*0.15);
- AcGeVector3d Normal2(-Normal.y,Normal.x,0);
- AcGePoint3d Point3(Start+Length*Normal*0.5+Length*Normal2*0.10);
- AcGePoint3d Point4(Start+Length*Normal*0.5-Length*Normal2*0.10);
- AcGePoint3dArray vertices;
- vertices.append(Point1);
- vertices.append(Point2);
- vertices.append(Point3);
- vertices.append(Point4);
- vertices.append(Point5);
- vertices.append(Point6);
- AddNewLayer("COMMANTARY");
- AcDb2dPolyline* pBreakLine = new AcDb2dPolyline(AcDb::k2dSimplePoly,vertices,0,Adesk::kTrue,0,0,NULL);
- pBreakLine->setLayer("COMMANTARY",TRUE);
- AcGeMatrix3d mat;
- acdbUcsMatrix(mat,acdbHostApplicationServices()->workingDatabase());
- pBreakLine->transformBy(mat);
- pBreakLine->makeOpen();
- AddEntityToDb(pBreakLine);
- }
- //******************生成回转体**********************
- /* pt -- 旋转基点
- ver -- 旋转轴
- angle -- 旋转角度(角度制)
- 注意: 旋转轴不能垂直于面域平面、不能穿过面域*/
- //**************************************************
- void CreatRevolve(AcDbObjectId entid,
- AcGeVector3d normal,
- AcGePoint3d pt,
- AcGeVector3d ver,
- double angle)
- {
- Acad::ErrorStatus es;
- AcDbCurve *curve;
- AcDbObjectId tm;
- if (acdbOpenObject(curve,entid,AcDb::kForWrite)!=Acad::eOk)
- {
- acutPrintf("打开实体失败!");
- return ;
- }
- AcDbVoidPtrArray lines,regions1;
- lines.append((void*)curve);
- curve->close();
- es = AcDbRegion::createFromCurves(lines,regions1);
- if(es != Acad::eOk)
- {
- acutPrintf("获得面域失败!");
- return ;
- }
- angle = angle*PI/180;
- AcDbRegion *pregion1=AcDbRegion::cast((AcRxObject*)regions1[0]);
- AcDb3dSolid *p3dobj = new AcDb3dSolid;
- es = p3dobj->revolve(pregion1,pt,ver,angle);
- if (es != Acad::eOk)
- {
- acutPrintf("建立回转体失败!请检查回转轴和基准点是否正确!");
- }
- pBlockTableRecord->appendAcDbEntity(tm,p3dobj);
- p3dobj->close();
- delete pregion1;
- }
- //复制对象
- void cloneSameOwnerObjects()
- {
- // Step 1: Obtain the set of objects to be cloned.
- ads_name sset;
- if (acedSSGet(NULL, NULL, NULL, NULL, sset) != RTNORM) {
- acutPrintf("\nNothing selected");
- return;
- }
- // Step 2: Add obtained object IDs to list of objects
- // to be cloned.
- long length;
- acedSSLength(sset, &length);
- AcDbObjectIdArray objList;
- AcDbObjectId ownerId = AcDbObjectId::kNull;
- for (int i = 0; i < length; i++) {
- ads_name ent;
- acedSSName(sset, i, ent);
- AcDbObjectId objId;
- acdbGetObjectId(objId, ent);
- // Check to be sure this has the same owner as the first
- // object.
- //
- AcDbObject *pObj;
- acdbOpenObject(pObj, objId, AcDb::kForRead);
- if (pObj->ownerId() == ownerId)
- objList.append(objId);
- else if (i == 0) {
- ownerId = pObj->ownerId();
- objList.append(objId);
- }
- pObj->close();
- }
- acedSSFree(sset);
- // Step 3: Get the object ID of the desired owner for
- // the cloned objects. We'll use model space for
- // this example.
- //
- AcDbBlockTable *pBlockTable;
- acdbHostApplicationServices()->workingDatabase()
- ->getSymbolTable(pBlockTable, AcDb::kForRead);
- AcDbObjectId modelSpaceId;
- pBlockTable->getAt(ACDB_MODEL_SPACE, modelSpaceId);
- pBlockTable->close();
- // Step 4: Create a new ID map.
- //
- AcDbIdMapping idMap;
- // Step 5: Call deepCloneObjects().
- //
- acdbHostApplicationServices()->workingDatabase()
- ->deepCloneObjects(objList, modelSpaceId, idMap);
- // Now we can go through the ID map and do whatever we'd
- // like to the original and/or clone objects.
- //
- // For this example, we'll print out the object IDs of
- // the new objects resulting from the cloning process.
- //
- AcDbIdMappingIter iter(idMap);
- for (iter.start(); !iter.done(); iter.next()) {
- AcDbIdPair idPair;
- iter.getMap(idPair);
- if (!idPair.isCloned())
- continue;
- acutPrintf("\nObjectId is: %Ld",
- idPair.value().asOldId());
- }
- }
- 线和面求交,线和三角形交.
- bool InterSection(XRay& ray,XPlan& plan,XPoint& point,float & t)
- {
- t = 0;
- /***
- -D - P0 * N(A,B,C)
- t = --------------------
- d * N(A,B,C)
- 其中D 为平面方程中的D,
- P0为射线的起点。而 d 为射线的方向
- **/
- t= -(plan.D + ray.m_Point.x * plan.A + ray.m_Point.y * plan.B + ray.m_Point.z * plan.C);
- float t2 = (ray.m_Dir.x * plan.A + ray.m_Dir.y * plan.B + ray.m_Dir.z * plan.C) ;
- if(t2 == 0.00)
- return false;
- t /= t2;
- //求出交点
- point = ray.m_Point + ray.m_Dir * t;
- if( t < 0)
- return false;
- return true;
- }
- bool InterSection(XRay& ray,XTriangle& tri,XPoint& point,float & t)
- {
- XVector3D v1 = tri.m_points[1] - tri.m_points[0];
- XVector3D v2 = tri.m_points[2] - tri.m_points[0];
- XVector3D n = v1.cp(v2);
- /**********************************************************************
- ray = p0 + D * t
- Tri : p1 - p3
- (P2-P1) X (P3-P1) = N
- N * (P - P1) = 0 ===>
- N * (P0 + D * t - P1) = 0
- ==>
- N * (P0 - P1) + N * D * t = 0;
- (P1-P0) * N
- t = ----------------------
- D * N
- ******************************************************************/
- t = n.dp(tri.m_points[0] - ray.m_Point);
- t /= (ray.m_Dir.dp(n));
- if( t < 0)
- return false;
- //求出交点
- point = ray.m_Point + ray.m_Dir * t;
- //判断点是不是在三角内部
- v1 = point - tri.m_points[0];
- v2 = point - tri.m_points[1];
- XVector3D v3 = point - tri.m_points[2];
- n = v1.cp(v2);
- XVector3D n1 = v2.cp(v3);
- if(n.isZero())
- return true;
- if(n1.isZero())
- return true;
- if( n.x * n1.x <= 0 )
- return false;
- if( n.y * n1.y <= 0 )
- return false;
- if( n.z * n1.z <= 0 )
- return false;
- return true;
- }
- //命名对象词典中无所需生成的词典(dictName)时生成词典,当DdictName存在时直接返回
- void createDictionary(char *dictName)
- {
- AcDbDictionary *pNameDict;
- acdbHostApplicationServices()->workingDatabase()->
- getNamedObjectsDictionary(pNameDict, AcDb::kForWrite);
- AcDbDictionary *pDict;
- if (pNameDict->getAt(dictName, (AcDbObject*&) pDict,
- AcDb::kForWrite) == Acad::eKeyNotFound)
- {
- pDict = new AcDbDictionary;
- AcDbObjectId DictId;
- pNameDict->setAt(dictName, pDict, DictId);
- }
- pNameDict->close();
- pDict->close();
- }
- //将自定义对象pObj加入词典dictName,对象名为objectName。词典dictName不存在时,调用creatDictionary()生成。
- void addObjectToDict(AcDbObject *pObj,char *objectName,char *dictName)
- {
- createDictionary(dictName);
- AcDbDictionary *pNamedobj;
- acdbHostApplicationServices()->workingDatabase()
- ->getNamedObjectsDictionary(pNamedobj, AcDb::kForRead);
- Acad::ErrorStatus es;
- AcDbDictionary *pDict;
- es=pNamedobj->getAt(dictName, (AcDbObject*&)pDict,
- AcDb::kForWrite);
- pNamedobj->close();
- if (es==Acad::eOk) //使用if(pDict)不能起到判断的作用。
- {
- // add new objects to the dictionary,
- // then close them.
- AcDbObjectId rId1;
- pDict->setAt(objectName, pObj, rId1);
- pDict->close(); //不能放在该处{}之外,即未能正常获取,就无所谓关闭,否则出错。
- }
- // pDict->close();
- }
- //根据CAD两点坐标及对应的大地坐标(经纬距)计算两坐标系的夹角angle及比例转换系数K
- //计算大地坐标系原点在CAD世界坐标系中的原点坐标earthorg
- //函数return夹角,其单位为弧度
- float EonAndXoyAngle(ads_point cadPt1,ads_point cadPt2,ads_point earthPt1,ads_point earthPt2,double &k,ads_point &earthOrg)
- {
- float angle;
- double n,e,x,y;
- float ax,ae;
- n=(earthPt2[Y]-earthPt1[Y]);
- e=(earthPt2[X]-earthPt1[X]);
- x=(cadPt2[X]-cadPt1[X]);
- y=(cadPt2[Y]-cadPt1[Y]);
- k=sqrt((n*n+e*e)/(x*x+y*y));
- ax=atan2(y,x); //线段与X轴的夹角
- ae=atan2(n,e); //线段与E轴的夹角
- angle=(ax-ae);
- earthOrg[X]=cadPt1[X]-(cos(angle)*earthPt1[X]-sin(angle)*earthPt1[Y])/k;
- earthOrg[Y]=cadPt1[Y]-(cos(angle)*earthPt1[Y]+sin(angle)*earthPt1[X])/k;
- return angle;
- }
- //将CAD坐标转换为大地坐标
- void xyToEn(ads_point cadPt,ads_point& earthPt,ads_point earthOrg,float angle, double k)
- {
- earthPt[X]=k*(cos(angle)*(cadPt[X]-earthOrg[X])+sin(angle)*(cadPt[Y]-earthOrg[Y]));
- earthPt[Y]=k*(-sin(angle)*(cadPt[X]-earthOrg[X])+cos(angle)*(cadPt[Y]-earthOrg[Y]));
- }
- //将大地坐标转换为CAD坐标
- void enToXY(ads_point earthPt,ads_point& cadPt,ads_point earthOrg,float angle, double k)
- {
- cadPt[X]=earthOrg[X]+(cos(angle)*earthPt[X]-sin(angle)*earthPt[Y])/k;
- cadPt[Y]=earthOrg[Y]+(cos(angle)*earthPt[Y]+sin(angle)*earthPt[X])/k;
- }
- // 判定当前图形中有无给定块名的块表记录,有返回true、同时返回其id,
- //否则返回flase
- bool existBlock(char* pBlockName,AcDbObjectId &blockId)
- {
- AcDbBlockTable* pBlockTable;
- acdbHostApplicationServices()->workingDatabase()
- ->getSymbolTable(pBlockTable,AcDb::kForRead);
- if(pBlockTable->has(pBlockName))
- {
- pBlockTable->getAt(pBlockName,blockId);
- pBlockTable->close();
- return true;
- }
- else
- {
- pBlockTable->close();
- return false;
- }
- }
- // 把一图形文件dwg插入当前图形作为一块,并给予块名。若有同名块就不再插入。
- bool insertDwgToCurDwg(char* pFullPathFile,char* pBlockName,AcDbObjectId &blockId)
- {
- AcDbDatabase *pNewDb,*pCurDb;
- Acad::ErrorStatus es;
- if(existBlock(pBlockName,blockId))
- {
- return true;
- }
- pNewDb=new AcDbDatabase(Adesk::kFalse);
- es=pNewDb->readDwgFile(pFullPathFile, _SH_DENYNO,false);
- if (es!=Acad::eOk)
- {
- acutPrintf("读文件错");
- delete pNewDb;
- return false;
- }
- pCurDb =acdbHostApplicationServices()->workingDatabase();
- if((es=pCurDb->insert(blockId, pBlockName,pNewDb, true))==Acad::eOk)
- {
- delete pNewDb;
- return true;
- }
- else
- {
- delete pNewDb;
- return false;
- }
- }
- AcGePlane
- CRegionGraph::getUcsPlane(AcDbDatabase* db)
- {
- ASSERT(db != NULL);
- AcGeMatrix3d m;
- if (acdbUcsMatrix(m, db)) {
- AcGePoint3d origin;
- AcGeVector3d xDir, yDir, zDir;
- m.getCoordSystem(origin, xDir, yDir, zDir);
- AcGePlane ucsPlane(origin, xDir, yDir);
- return ucsPlane;
- }
- else {
- ASSERT(0);
- AcGePlane ucsPlane(AcGePoint3d::kOrigin, AcGeVector3d::kIdentity);
- return ucsPlane;
- }
- }
- //*****************************
- AcGeVector3d
- CRegionGraph::getUcsXAxis(AcDbDatabase* db)
- {
- ASSERT(db != NULL);
- AcGeMatrix3d m;
- if (acdbUcsMatrix(m, db)) {
- AcGePoint3d origin;
- AcGeVector3d xDir, yDir, zDir;
- m.getCoordSystem(origin, xDir, yDir, zDir);
- return xDir;
- }
- else {
- ASSERT(0);
- return AcGeVector3d::kXAxis;
- }
- }
- /****************************************************************************
- **
- ** CRegionGraph::getUcsYAxis
- ** returns current UCS Y Axis, even if currently the paperSpace viewport
- **
- ** **jma
- */
- //*************************************/
- AcGeVector3d
- CRegionGraph::getUcsYAxis(AcDbDatabase* db)
- {
- ASSERT(db != NULL);
- AcGeMatrix3d m;
- if (acdbUcsMatrix(m, db)) {
- AcGePoint3d origin;
- AcGeVector3d xDir, yDir, zDir;
- m.getCoordSystem(origin, xDir, yDir, zDir);
- return yDir;
- }
- else {
- ASSERT(0);
- return AcGeVector3d::kYAxis;
- }
- }
- /****************************************************************************
- **
- ** CRegionGraph::getUcsZAxis
- **
- ** **jma
- **
- *************************************/
- AcGeVector3d
- CRegionGraph::getUcsZAxis(AcDbDatabase* db)
- {
- ASSERT(db != NULL);
- AcGeMatrix3d m;
- if (acdbUcsMatrix(m, db)) {
- AcGePoint3d origin;
- AcGeVector3d xDir, yDir, zDir;
- m.getCoordSystem(origin, xDir, yDir, zDir);
- return zDir;
- }
- else {
- ASSERT(0);
- return AcGeVector3d::kZAxis;
- }
- }
- //指定两点Pnt3d1,Pnt3d2,查询Pnt3d3是否在由前两点组成的线的左边,坐标被投影成二维
- BOOL CCommonHanlder::QueryPntIsOnLineLeft(AcGePoint3d Pnt3d1,AcGePoint3d Pnt3d2,AcGePoint3d Pnt3d3)
- {
- AcGePlane plane;
- AcGePoint2d Pnt1,Pnt2,Pnt3;
- Pnt1=Pnt3d1.convert2d(plane);
- Pnt2=Pnt3d2.convert2d(plane);
- Pnt3=Pnt3d3.convert2d(plane);
- AcGeVector2d vec1,vec2;
- AcGeLineSeg2d lin1,lin2;
- lin1.set(Pnt2,Pnt1);
- vec1=lin1.direction();
- lin2.set(Pnt1,Pnt3);
- vec2=lin2.direction();
- if(sin(vec1.angle()-vec2.angle())>0)
- return FALSE;
- else
- return TRUE;
- }
- /////////////////////创建窗体图层"Window_Layer"/////////////////
- AcDbObjectId
- createWindowsLayer()
- {
- //打开层表,打开方式为只写///
- AcDbLayerTable *pLayerTable;
- acdbHostApplicationServices()->workingDatabase()
- ->getSymbolTable(pLayerTable,AcDb::kForWrite);
- //初始化层表记录对象,并设定层表记录的名称“Window_Layer”
- AcDbLayerTableRecord *pLayerTableRecord=new AcDbLayerTableRecord;
- pLayerTableRecord->setName("Window_Layer");
- AcCmColor color;
- color.setColorIndex(1); // set color to red
- pLayerTableRecord->setColor(color);
- //层的其他属性(线型等)都用缺省值////
- //将新建的层表记录添加到层表中,并将层表记录的ID保存到pLayerId作为函数的返回值////
- AcDbObjectId pLayerId;
- pLayerTable->add(pLayerId,pLayerTableRecord);
- //关闭层表和层表记录对象///
- pLayerTable->close();
- pLayerTableRecord->close();
- return pLayerId;
- }
- ///*************在指定点插入指定块*******************\\\
- void CTestPlate::OnBlockInsert()
- {
- // TODO: Add your control notification handler code here
- acDocManager->lockDocument(curDoc());
- AcDbObjectId blockId; //要插入的块的Id值
- AcDbBlockTable *pBlockTable;
- acdbHostApplicationServices()->workingDatabase()
- ->getSymbolTable(pBlockTable,AcDb::kForRead);
- Acad::eOk!=pBlockTable->getAt("ASDK-NO-ATTR",
- blockId,AcDb::kForRead)//根据块名获得要插入的块的ID值
- AcDbBlockReference *pBlkRef=new AcDbBlockReference;//插入块实质是插入块的引用
- pBlkRef->setBlockTableRecord(blockId);//指定所引用的图块表记录的对象ID
- resbuf to,from;
- from.restype=RTSHORT;//插入图块要进行用户坐标与世界坐标的转换
- from.resval.rint=1;//UCS
- to.restype=RTSHORT;
- to.resval.rint=0;//WCS
- AcGeVector3d normal(0.0,0.0,1.0);
- acedTrans(&(normal.x),&from,&to,Adesk::kTrue,&(normal.x));//转换函数
- AcGePoint3d basePoint(12,23,0);//指定的插入点(可以根据需要输入)
- //acedGetPoint(NULL,"\nEnter insertion point:",asDblArray(basePoint));
- pBlkRef->setPosition(basePoint);
- pBlkRef->setRotation(0.0);
- pBlkRef->setNormal(normal);
- AcDbBlockTableRecord *pBlockTableRecord;
- pBlockTable->getAt(ACDB_MODEL_SPACE,pBlockTableRecord,AcDb::kForWrite);
- pBlockTable->close();
- AcDbObjectId newEntId;
- pBlockTableRecord->appendAcDbEntity(newEntId,pBlkRef);
- pBlockTableRecord->close();
- pBlkRef->close();
- acDocManager->lockDocument(curDoc());
- }
- //點遷多義線中之一直線,獲得線段之位置(圓弧可自行補上)
- void GetPolyLineSelectLineIndex(AcDbPolyline *Poly,AcGePoint3d SelectPt,int Index)
- {
- AcGePoint2d tempPt;
- double Distance=11111111;
- tempPt.x=SelectPt.x;
- tempPt.y=SelectPt.y;
- AcDbVoidPtrArray entitySet;
- Poly->explode(entitySet);
- for(int i=0;i<ENTITYSET.LENGTH();I++)
- {
- AcDbEntity *pEnt;
- pEnt=AcDbPolyline::cast((AcRxObject*)entitySet[i]);
- if(pEnt->isA()==AcDbLine::desc())
- {
- AcDbLine *pLine=AcDbLine::cast(pEnt);
- AcGePoint2d StPt,EndPt;
- StPt.x=pLine->startPoint().x;
- StPt.y=pLine->startPoint().y;
- EndPt.x=pLine->endPoint().x;
- EndPt.y=pLine->endPoint().y;
- pLine->close();
- AcGeLine2d GeLine(StPt,EndPt);
- double pama=0;
- if((GeLine.isOn(tempPt,pama)==Adesk::kTrue)&&pama<=1&&pama>=0)
- {
- Index=i;
- break;
- }
- else
- {
- if(GeLine.distanceTo(tempPt)<DISTANCE)
- {
- AcGeLine2d tempLine;
- GeLine.getPerpLine(tempPt,tempLine);
- AcGePoint2d InsertPt;
- GeLine.intersectWith(tempLine,InsertPt);
- double pama=0;
- if((GeLine.isOn(InsertPt,pama)==Adesk::kTrue)&&pama<=1&&pama>=0)
- {
- Distance=GeLine.distanceTo(tempPt);
- Index=i;
- }
- }
- }
- }
- pEnt->close();
- }
- }
- int unionSolid(AcDbObjectId extrudeId,AcDb3dSolid *&pSolid)
- {
- //打开切弧实体 pExtrude
- AcDb3dSolid *pExtrude;
- if (Acad::eOk!=acdbOpenObject(pExtrude,extrudeId,AcDb::kForWrite)){
- acutPrintf("\nError: Invalid extrude solid! ");
- if(pExtrude!=NULL) pExtrude->close();
- return RTERROR;
- }
- if(pExtrude==NULL){
- acutPrintf("\nError: Invalid extrude solid! ");
- return RTERROR;
- }
- // 实体
- if(pSolid==NULL){
- acutPrintf("\nError: Invalid extrude solid! ");
- return RTERROR;
- }
- //Unit
- if(Acad::eOk!=pSolid->booleanOper(AcDb::kBoolUnite,pExtrude))
- {
- acutPrintf("\nError: substract error!");
- pExtrude->close();
- return RTERROR;
- }
- pExtrude->close();
- return RTNORM;
- }
- Acad::ErrorStatus DyZoom(AcGePoint2d CenterPt,double Height,double Width)
- {
- Acad::ErrorStatus es;
- AcDbViewTableRecord *view=new AcDbViewTableRecord();
- view->setCenterPoint(CenterPt);
- view->setFrontClipAtEye(false);
- view->setHeight(Height);
- view->setWidth(Width);
- es=acedSetCurrentView(view,NULL);
- view->close();
- delete view;
- view=NULL;
- return es;
- }
- 创建circle和newlayer(加了错误检查的代码)
- Acad::ErrorStatus
- createCircle(AcDbObjectId& circleId)
- {
- circleId = AcDbObjectId::kNull;
- AcGePoint3d center(9.0,3.0,0.0);
- AcGeVector3d normal(0.0,0.0,0.1);
- AcDbCircle*pCirc=new AcDbCircle(center,normal,2.0);
- if(pCirc==NULL)
- return Acad::eOutOfMemory;
- AcDbBlockTable*pBlockTable;
- Acad::ErrorStatus es=
- acdbHostApplicationServices()->workingDatabase()->
- getSymbolTable(pBlockTable,AcDb::kForRead);
- if(es!=Acad::eOk)
- {
- delete pCirc;
- return es;
- }
- AcDbBlockTableRecord*pBlockTableRecord;
- es=pBlockTable->getAt(ACDB_MODEL_SPACE,pBlockTableRecord,AcDb::kForWrite);
- if(es!=Acad::eOk){
- Acad::ErrorStatus es2=pBlockTable->close();
- if (es2!=Acad::eOk){
- acrx_abort("\n应用程序X关闭边失败,错误码为:%d",
- acadErrorStatusText(es2));
- }
- delete pCirc;
- return es;
- }
- es=pBlockTable->close();
- if (es!=Acad::eOk)
- {
- acrx_abort("\n应用程序X关闭边失败,错误码为:%d",
- acadErrorStatusText(es));
- }
- es=pBlockTableRecord->appendAcDbEntity(circleId,pCirc);
- if(es!=Acad::eOk){
- Acad::ErrorStatus es2=pBlockTableRecord->close();
- if (es2!=Acad::eOk){
- acrx_abort("\n应用程序X关闭模型空间块表记录失败,错误码为:%s",
- acadErrorStatusText(es2));
- }
- delete pCirc;
- return es;
- }
- es=pBlockTableRecord->close();
- if(es!=Acad::eOk)
- {
- acrx_abort("\n应用程序X关闭模型空间块表记录失败,错误码为:%d",
- acadErrorStatusText(es));
- }
- es=pCirc->close();
- if(es!=Acad::eOk){
- acrx_abort("\n应用程序X关闭实体失败,错误码为:%d",
- acadErrorStatusText(es));
- }
- return es;
- }
- Acad::ErrorStatus
- createNewLayer()
- {
- AcDbLayerTableRecord*pLayerTableRecord=new AcDbLayerTableRecord;
- if (pLayerTableRecord==NULL)
- return Acad::eOutOfMemory;
- Acad::ErrorStatus es
- =pLayerTableRecord->setName("ASDK_MYLAYER");
- if(es!=Acad::eOk)
- {
- delete pLayerTableRecord;
- return es;
- }
- AcDbLayerTable*pLayerTable;
- es=acdbHostApplicationServices()->workingDataBase()->
- getSymbolTable(pLayerTable,AcDb::kForWrite);
- if(es!=Acad::eOk){
- delete pLayerTableRecord;
- return es;
- }
- //由于先型对象ID的缺省直是0,不是有效的ID,必须将其设置为有效的ID,
- //即CONTINUOUS线型。其他数据成员都为有效缺省值,因此不须对其进行设置
- AcDbLinetypeTable*pLinetypeTbl;
- es=acdbHostApplicationServices()->workingDatabase()->
- getSymbolTable(pLinetypeTbl,AcDb::kForRead);
- if (es!=Acad::eOk){
- delete pLayerTableRecord;
- es=pLayerTable->close();
- if(es!=Acad::eOk){
- acrx_abort("\n应用程序X关闭实体失败,错误码为:%d",
- acadErrorStatusText(es));
- }
- return es;
- }
- AcDbObjectId ltypeobjId;
- es=pLinetypeTbl->getAt("CONTINUOUS",ltypeObjId);
- if (es!=Acad::eOk){
- delete pLayerTableRecord;
- es=pLayerTable->close();
- if (es!=Acad::eOk){
- acrx_abort("\n应用程序X关闭实体失败,错误码为:%d",acadErrorStatusText(es));
- }
- return es;
- }
- pLayerTableRecord->setLinetypeObjectId(ltypeObjId);
- es=pLayerTable->add(pLayerTableRecord);
- if (es!=Acad::eOk)
- {
- Acad::ErrorStatus es2=pLayerTable->close();
- if (es2!=Acad::eOk)
- {
- acrx_abort("\n应用程序X关闭实体失败,错误码为:%d",
- acadErrorStatusText(es2));
- }
- delete pLayerTableRecord;
- return es;
- }
- es=pLayerTable->close();
- if(es!=Acad::eOk)
- {
- acrx_abort("\n应用程序X关闭实体失败,错误码为:%d",
- acadErrorStatusText(es));
- }
- es=pLayerTableRecord->close();
- if(es!=Acad::eOk)
- {
- acrx_abort("\n应用程序X关闭实体失败,错误码为:%d",
- acadErrorStatusText(es));
- }
- return es;
- }
- Function name: setEntityElevation
- Parameters: ads_name, double
- Return value: False or True
- According to the Entity name setting the elevation value of text,polyline,2dpolyline,block,hatch which is given
- BOOL setEntityElevation(ads_name en, double elevation)
- { AcDbEntity* pEnt;
- AcDbObjectId eId;
- if(acdbGetObjectId(eId, en) != Acad::eOk) return FALSE;
- if(acdbOpenObject(pEnt,eId,AcDb::kForWrite) != Acad::eOk) return FALSE;
- if(!pEnt) { acutPrintf("\nFail to open Database!!"); return FALSE; }
- if(pEnt->isKindOf(AcDbPolyline::desc())) {
- ((AcDbPolyline*)pEnt)->setElevation(elevation);
- pEnt->close(); // Finished with the pEnt header.
- }
- else if(pEnt->isKindOf(AcDb2dPolyline::desc())) {
- ((AcDb2dPolyline*)pEnt)->setElevation(elevation);
- pEnt->close(); // Finished with the pEnt header.
- }
- else if(pEnt->isKindOf(AcDbText::desc())) {
- AcGePoint3d point3d;
- point3d = ((AcDbText*)pEnt)->position();
- point3d[2] = elevation;
- ((AcDbText*)pEnt)->setPosition(point3d);
- pEnt->close(); // Finished with the pEnt header.
- }
- else if(pEnt->isKindOf(AcDbMText::desc())) {
- AcGePoint3d point3d;
- point3d = ((AcDbMText*)pEnt)->location();
- point3d[2] = elevation;
- ((AcDbMText*)pEnt)->setLocation(point3d);
- pEnt->close(); // Finished with the pEnt header.
- }
- else if(pEnt->isKindOf(AcDbBlockReference::desc())) {
- AcGePoint3d point3d;
- point3d = ((AcDbBlockReference*)pEnt)->position();
- point3d[2] = elevation;
- ((AcDbBlockReference*)pEnt)->setPosition(point3d);
- pEnt->close(); // Finished with the pEnt header.
- }
- else if(pEnt->isKindOf(AcDbShape::desc())) {
- AcGePoint3d point3d;
- point3d = ((AcDbShape*)pEnt)->position();
- point3d[2] = elevation;
- ((AcDbShape*)pEnt)->setPosition(point3d);
- pEnt->close(); // Finished with the pEnt header.
- }
- else if(pEnt->isKindOf(AcDbHatch::desc())) {
- ((AcDbHatch*)pEnt)->setElevation(elevation);
- pEnt->close(); // Finished with the pEnt header.
- }
- else { pEnt->close(); return FALSE; }
- return TRUE;
- 推荐使用方法二,基本上适用于AutoCAD所有版本(我可是跟踪分析注册表,费了好大一番劲才搞出来的哟)。"W-SCAS2006"为/p参数启动的配置名称。
- 只要在退出消息里调用这个函数就行,如:在kPreQuitMsg消息里。
- /*case AcRx::kPreQuitMsg:
- REStoreDefPro();
- break;*/
- //////////////////////////////////////////////////////////////////////////
- // 函数名: REStoreDefPro
- // 输 入: /
- // 输 出: /
- // 功能描述: 恢复当前配置文件为AutoCAD默认配置
- // 全局变量: /
- // 调用模块:
- // 作 者: Meng Huang
- // 日 期: 2006.02.21
- // 修 改: Meng Huang
- // 日 期: 2006.02.23
- // 版 本: 1.1
- //////////////////////////////////////////////////////////////////////////
- void REStoreDefPro()
- {
- // 方法一:
- // AcApProfileNameArray arrNameList;
- // int nProfiles = acProfileManagerPtr()->ProfileListNames(arrNameList);
- // CString strProName;
- // Acad::ErrorStatus es;
- //
- // for (int i = 0; i < nProfiles; i++)
- // {
- // strProName = arrNameList[i];
- // if (strProName == _T("<<未命名配置>>") ||
- // strProName == _T("<>"))
- // {
- // /*es = */acProfileManagerPtr()->ProfileSetCurrent(strProName);
- // break;
- // }
- // else if (i == nProfiles - 1)
- // {
- // /*es = */acProfileManagerPtr()->ProfileCopy(_T("<<未命名配置>>"), _T("W-SCAS2006"), _T("默认配置名称"));
- // /*es = */acProfileManagerPtr()->ProfileReset(_T("<<未命名配置>>"));
- // /*es = */acProfileManagerPtr()->ProfileSetCurrent(_T("<<未命名配置>>"));
- // }
- // }
- // 方法二:
- HKEY hKey;
- LONG lRetVal;
- DWORD unSubKeyNum = 0;
- DWORD sizeSubKey; // 子键大小
- TCHAR subKeyName[255]; // 子键名
- // 打开HKEY_USERS主键
- lRetVal = RegOpenKeyEx(HKEY_USERS, _T(""), 0, KEY_READ, &hKey);
- if (lRetVal != ERROR_SUCCESS)
- return;
- // 查得当前键下的子键项数
- lRetVal = RegQueryInfoKey(hKey, NULL, NULL, NULL, &unSubKeyNum, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
- if (lRetVal != ERROR_SUCCESS)
- {
- RegCloseKey(hKey);
- return;
- }
- // 取得含有S-1-5-21-***********,但不包含Classes字符串的子键名
- CString strTmp;
- CString strFullSubKey;
- for (DWORD i = 0; i < unSubKeyNum; i++)
- {
- subKeyName[0] = '\0'; // 清空子键
- sizeSubKey = 255; // 赋缓冲区大小
- lRetVal = RegEnumKeyEx(hKey, i, subKeyName, &sizeSubKey, NULL, NULL, NULL, NULL);
- if (lRetVal != ERROR_SUCCESS)
- {
- RegCloseKey(hKey);
- return;
- }
- strTmp = subKeyName;
- if (strTmp.Find(_T("S-1-5-21-")) != -1 &&
- strTmp.Find(_T("Classes")) == -1)
- {
- strFullSubKey = strTmp;
- break;
- }
- }
- if (strFullSubKey.IsEmpty()) // 没有找到符合条件的子键
- {
- RegCloseKey(hKey);
- return;
- }
- // 获取和配置文件相对应的的子键字符串
- char *pchRegProfileKey;
- acProfileManagerPtr()->ProfileRegistryKey(pchRegProfileKey, _T("W-SCAS2006"));
- if (pchRegProfileKey == NULL)
- {
- RegCloseKey(hKey);
- return;
- }
- else
- {
- strTmp = pchRegProfileKey;
- acutDelString(pchRegProfileKey);
- int nStart = strTmp.Find('\');
- int nEnd = strTmp.ReverseFind('\');
- strTmp = strTmp.Mid(nStart, nEnd - nStart);
- RegCloseKey(hKey);
- }
- strFullSubKey += strTmp; // 要修改键值的完整子键字符串
- // 修改对应的键值(把该键值置空,下次启动CAD如果没指定/p参数则使用<<未命名配置>>,
- // 如果该配置不在,CAD会自动新一个,并使用它)
- lRetVal = RegOpenKeyEx(HKEY_USERS, strFullSubKey, 0, KEY_WRITE, &hKey);
- if (lRetVal != ERROR_SUCCESS)
- {
- RegCloseKey(hKey);
- return;
- }
- BYTE uchProFile[1] = {'\0'};
- RegSetValueEx(hKey, NULL, 0, REG_SZ, uchProFile, 1);
- RegCloseKey(hKey);
- }
- //************************************************************************
- //函数名称:selectEntityInLayer
- //函数类型:Acad::ErrorStatus
- //返回值: 正常:Acad::eOk
- //功能描述:选择指定图层上的所有实体!
- //函数作者:Darcy
- //创建日期:200X-XX-XX
- //参数列表:
- //变量名:nLayerName 变量类型:const char* 变量说明:(输入)图层名
- //变量名:nIDs 变量类型:AcDbObjectIdArray& 变量说明:(输出)图层中实体的对象标识符集合
- //************************************************************************
- Acad::ErrorStatus selectEntityInLayer(const char* nLayerName,AcDbObjectIdArray& nIDs)
- {
- Acad::ErrorStatus es = Acad::eOk;
- ads_name ents;
- struct resbuf *rb;
- rb=acutNewRb(AcDb::kDxfLayerName);
- rb->restype=8;
- rb->resval.rstring=(char*)nLayerName;
- rb->rbnext=NULL;
- acedSSGet("X",NULL,NULL,rb,ents);
- long entNums=0;
- acedSSLength(ents,&entNums);
- if (entNums == 0)
- es = Acad::eInvalidInput;
- else
- {
- for (long a = 0; a < entNums ; a ++)
- {
- AcDbObjectId objId;
- ads_name ent;
- acedSSName(ents,a,ent);
- acdbGetObjectId(objId, ent);
- nIDs.append(objId);
- }
- }
- acedSSFree(ents);
- acutRelRb(rb);
- return es;
- }
- //创建字体类型
- 代码:
- void DrawAndEdit::createTextStyle(char *styleName, char *fontName, char *bigFontName, double textSize, double xScale, double obliqueAngle, double trPercent)
- {
- AcDbTextStyleTable *pTextStyleTable;
- acdbHostApplicationServices()->workingDatabase()
- ->getSymbolTable(pTextStyleTable, AcDb::kForRead);
- AcDbTextStyleTableRecord *pRecord;
- pTextStyleTable->getAt(ACDB_MODEL_SPACE,pRecord,
- AcDb::kForWrite);
- if (!pTextStyleTable->has(styleName)){
- AcDbObjectId recId;
- pTextStyleTable->close();
- acdbHostApplicationServices()->workingDatabase()
- ->getSymbolTable(pTextStyleTable, AcDb::kForWrite);
- pRecord = new AcDbTextStyleTableRecord();
- pRecord->setName(styleName);
- pRecord->setFileName(fontName);
- pRecord->setBigFontFileName(bigFontName);
- pRecord->setTextSize(textSize);
- pRecord->setXScale(xScale);
- pRecord->setObliquingAngle(obliqueAngle);
- pRecord->setPriorSize(trPercent);
- pTextStyleTable->add(recId,pRecord);
- pRecord->close();
- }
- pTextStyleTable->close();
- return;
- }
- //生成新组(sGroupName)
- //追加数组中所有实体到该组中
- //组名字 , Id数组
- int createGroup(CString sGroupName,
- const AcDbObjectIdArray *idArr)
- {
- AcDbGroup *pGroup = new AcDbGroup((LPSTR)(LPCTSTR)sGroupName);
- AcDbObjectId groupObjectId;
- AcDbDictionary *pGroupDict = NULL;
- acdbHostApplicationServices()->workingDatabase()
- ->getGroupDictionary(pGroupDict, AcDb::kForWrite);
- pGroupDict->setAt(sGroupName, pGroup, groupObjectId);
- pGroupDict->close();
- pGroup->close();
- acdbOpenObject(pGroup, groupObjectId, AcDb::kForWrite);
- for (int i = 0; i < idArr->length(); i++)
- {
- groupObjectId = idArr->at(i);
- pGroup->append(groupObjectId);
- }
- pGroup->close();
- return TRUE;
- }
- AcDbObjectId CreateNewTextStyle()
- {
- AcDbTextStyleTable *pTextStyleTable;
- AcDbTextStyleTableRecord *pTextStyleTableRcd
- AcDbObjectId textId;
- acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pTextStyleTable,AcDb::kForWrite);
- if (!pTextStyleTable->has(StyleName)
- {
- AcDbTextStyleTableRecord *pTSTblRcd= new AcDbTextStyleTableRecord;
- pTSTblRcd->setName(StyleName);
- pTSTblRcd->setFileName(fontName);
- pTSTblRcd->setBigFontFileName(bigfontName);
- pTSTblRcd->setTextSize(textSize);
- pTSTblRcd->setXScale(xScale);
- pTSTblRcd->setObliquingAngle(obliqueAngle);
- pTSTblRcd->setPriorSize(trPercent);
- pTextStyleTable->add(textId,pTextStyleTableRcd);
- pTSTblRcd->close();
- }
- pTextStyleTable->close();
- return textId;
- }
- void CMyDatabase::rotationGroup(CString strGroupName ,CReiPoint ptRotation,double rotationAngle)
- {
- AcGePoint3d pt;
- AcDbDictionary *pGroupDict;
- acdbCurDwg()->getGroupDictionary(pGroupDict,AcDb::kForWrite);
- AcDbObjectId groupId;
- AcDbGroup *pGroup;
- pt.x=ptRotation.x;
- pt.y=ptRotation.y;
- pt.z=ptRotation.z;
- if(pGroupDict->getAt(strGroupName,groupId)==Acad::eOk)
- acdbOpenObject(pGroup,groupId,AcDb::kForWrite);
- else
- {
- pGroupDict->close();
- return;
- }
- pGroupDict->close();
- AcDbGroupIterator* pIter=pGroup->newIterator();
- AcDbEntity* pEnt;
- AcDbObjectId objId;
- pIter=pGroup->newIterator();
- for(;!pIter->done();pIter->next())
- {
- objId=pIter->objectId();
- acdbOpenAcDbEntity(pEnt,objId,AcDb::kForWrite);
- rotationEntity(pEnt,pt,rotationAngle);
- pEnt->close();
- }
- delete pIter;
- pGroup->close();
- }
- //************************************************************************
- //函数名称:selectEntityInLayer
- //函数类型:Acad::ErrorStatus
- //返回值:
- //功能描述:选择指定层上的实体,得到其对象属性标识符!
- //函数作者:Darcy
- //创建日期:200X-XX-XX
- //参数列表:
- //变量名:nLayerName 变量类型:CString 变量说明:
- //变量名:nIDs 变量类型:AcDbObjectIdArray& 变量说明:
- //变量名:nModelSpace 变量类型:bool 变量说明:
- //************************************************************************
- Acad::ErrorStatus selectEntityInLayer(
- CString nLayerName,
- AcDbObjectIdArray& nIDs,
- bool nModelSpace
- )
- {
- Acad::ErrorStatus es=Acad::eOk;
- AcDbBlockTable* pBlockTable=NULL;
- AcDbBlockTableRecord* pSpaceRecord=NULL;
- if (acdbHostApplicationServices()->workingDatabase()==NULL)
- return Acad::eNoDatabase;
- if ((es = acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlockTable, AcDb::kForRead))==Acad::eOk)
- {
- char entryName[13];
- if (nModelSpace)
- strcpy(entryName,ACDB_MODEL_SPACE);
- else
- strcpy(entryName,ACDB_PAPER_SPACE);
- //Get the Model or Paper Space record and open it for read:
- if ((es = pBlockTable->getAt((const char*)entryName, pSpaceRecord, AcDb::kForRead))==Acad::eOk)
- {
- AcDbBlockTableRecordIterator* pIter;
- if (pSpaceRecord->newIterator(pIter)==Acad::eOk)
- {
- for (pIter->start();!pIter->done();pIter->step())
- {
- char *name=NULL;
- AcDbEntity* pEntity;
- if (pIter->getEntity(pEntity,AcDb::kForRead)==Acad::eOk)
- {
- name=pEntity->layer();
- if (nLayerName.CompareNoCase(name)==0)
- nIDs.append(pEntity->objectId());
- pEntity->close();
- acutDelString(name);
- }
- }
- delete pIter;
- }
- pSpaceRecord->close();
- }
- pBlockTable->close();
- }
- return es;
- }
- 功能: 判断两点是否相同(误差在指定范围内)
- 参数: p1、p2为欲判定的两点
- 返回值: 0 -- 不相同;1 -- 相同
- //=========================================================
- int Equal_Points (const ads_point p1, const ads_point p2)
- {
- // 指定误差范围
- const ads_real Equality_Margin = (ads_real)0.00000001;
-
- int c ;
- for (c = X ; c <= Z ; c++) {
- if (fabs(p1[c] - p2[c]) > Equality_Margin) {
- return (0) ;
- }
- }
- return (1) ;
- }
- BOOL BaseHandle::PointIsInPolygon(AcGePoint3d pt, AcGePoint3dArray ptArr)
- {
- int ptNum,i,interNum;
- AcGePoint3d ptA,ptB;
- ads_point pt0,pt1,pt2,ptIns,ptX;
-
- interNum = 0;
- pt0[X] = 0.0;
- pt0[Y] = 0.0;
- pt0[Z] = 0.0;
- ptX[X] = pt.x;
- ptX[Y] = pt.y;
- ptX[Z] = pt.z;
- ptNum = ptArr.length();
- for (i = 0;i < ptNum - 1;i++){
- ptA = ptArr.at(i);
- ptB = ptArr.at(i + 1);
- pt1[X] = ptA.x;
- pt1[Y] = ptA.y;
- pt1[Z] = 0.0;
- pt2[X] = ptB.x;
- pt2[Y] = ptB.y;
- pt2[Z] = 0.0;
- if (acdbInters(ptX,pt0,pt1,pt2,1,ptIns) == RTNORM){
- interNum++;
- }
- }
- //上述交点没有判断是否与某一端点重合,如果有interNum-1,有
- //兴趣的朋友可以修改完成。
- if (interNum % 2 == 0){
- return false;
- }else{
- return true;
- }
- }
- //==========================================================
- 功能:新建一个图层
- 参数:LayerName -- 图层名,LayerColor -- 颜色名
- 返回:图层ID
- //==========================================================
- AcDbObjectId CreateNewLayer(CString LayerName, AcCmColor LayerColor)
- {
- // 获得当前图形数据库的符号表
- AcDbLayerTable *pLayerTable;
- acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pLayerTable,
- AcDb::kForWrite);
- // 生成新的图层表记录
- AcDbLayerTableRecord *pLayerTableRecord = new AcDbLayerTableRecord;
- pLayerTableRecord->setName(LayerName); // 设置图层名
- pLayerTableRecord->setColor(LayerColor); // 设置图层颜色
- AcDbObjectId layerId;
- pLayerTable->add(layerId,pLayerTableRecord);
- // 关闭图层表和图层表记录
- pLayerTable->close();
- pLayerTableRecord->close();
- return layerId;
- }
- //==========================================================
- 功能:在指定图层上新建一条直线
- 参数:见注释
- 返回:直线ID
- //==========================================================
- AcDbObjectId CreateLine( double x1,double y1,double z1, // 起点坐标
- double x2,double y2,double z2, // 终点坐标
- AcDbObjectId layer) // 直线所在图层
- {
- AcGePoint3d StartPt(x1,y1,z1); // 起点
- AcGePoint3d EndPt(x2,y2,z2); // 终点
- AcDbLine *pLine = new AcDbLine(StartPt,EndPt);
- // 获得当前图形数据库的符号表
- AcDbBlockTable *pBlockTable;
- acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pBlockTable,
- AcDb::kForRead);
- // 获得符号表中的模型空间块表记录指针,用于添加对象
- AcDbBlockTableRecord *pBlockTableRecord;
- pBlockTable->getAt(ACDB_MODEL_SPACE,pBlockTableRecord,AcDb::kForWrite);
- pBlockTable->close();
- // 将直线添加到模型空间块表记录中
- AcDbObjectId lineId;
- pLine->setLayer(layer,Adesk::kTrue); // 设置直线所在图层
- pBlockTableRecord->appendAcDbEntity(lineId,pLine);
- // 关闭块表记录指针和直线指针
- pBlockTableRecord->close();
- pLine->close();
- // 返回直线ID号
- return lineId;
- }
- 已知一段弧的起点和终点以及其凸度,求其圆心
- int getCenter(ads_point startPoint,ads_point endPoint,double bulge,ads_point& center)
- {
- if (bulge==0.0)
- {
- ads_point_set(startPoint,center);
- return 0;
- }
- ads_point startPt,endPt;
- if (bulge<0.0)
- {
- ads_point_set(endPoint,startPt);
- ads_point_set(startPoint,endPt);
- bulge=bulge*(-1.0);
- }
- else
- {
- ads_point_set(startPoint,startPt);
- ads_point_set(endPoint,endPt);
- }
- double distStartToEnd,distX,distY,radius;
- distStartToEnd=ads_distance(startPt,endPt);
- distX=distStartToEnd/2.0;
- distY=bulge*distX;
- radius=((distX*distX)+(distY*distY))/(2.0*distY);
- double tmpAng;
- ads_point tmpPt;
- tmpAng=ads_angle(startPt,endPt);
- ads_polar(startPt,tmpAng,distX,tmpPt);
- ads_polar(tmpPt,(tmpAng+(_PI/2.0)),(radius-distY),center);
- return 1;
- };
|
评分
-
查看全部评分
|