admin 发表于 2024-2-12 22:37:46

objectarx自定义类函数

/******************************************************************
函数名:         setView
功能:         设置视图(相当于Zoom Window命令)
输入参数:       Pt1 -- 视图左上角点
          Pt2 -- 视图右下角点
          ex采用ratio -- 扩展比率,一般为1.0
输出参数:
返回值:         void
其它:
*****************************************************************/
void setView(AcGePoint2d Pt1, AcGePoint2d Pt2, double ex采用ratio)
{
AcGePoint2d CenterPt;

//若X坐标或Y坐标重合,判为意外,不进行SetView操作
if ((fabs(Pt1.x-Pt2.x)<1e-6)||(fabs(Pt1.y-Pt2.y)<1e-6))
return;

//确保两个坐标点分别为左上角和右下角
if (Pt1.x>Pt2.x) {
double tmp;
tmp = Pt1.x;
Pt1.x = Pt2.x;
Pt2.x = tmp;
}
if (Pt2.y>Pt1.y) {
double tmp;
tmp = Pt1.y;
Pt1.y = Pt2.y;
Pt2.y = tmp;
}

//获取当前DwgView的尺寸
CRect CADrect;
acedGetAcadDwgView()->GetClientRect(&CADrect);

double width,height,ratio;

ratio = (double)(CADrect.right-CADrect.left)/(double)(CADrect.bottom-CADrect.top);

if (fabs(ratio)<1e-6)
return;

if ((Pt2.x-Pt1.x)/(Pt1.y-Pt2.y) > ratio) {
width = Pt2.x-Pt1.x;
height = width/ratio;
}else{
height = Pt1.y-Pt2.y;
width = height * ratio;
}

//设置当前视图中心点
CenterPt.x = (Pt1.x+Pt2.x)/2;
CenterPt.y = (Pt1.y+Pt2.y)/2;

//改变当前视图
AcDbViewTableRecord pVwRec;
pVwRec.setCenterPoint(CenterPt);
pVwRec.setWidth(width * ex采用ratio);
pVwRec.setHeight(height * ex采用ratio);
acedSetCurrentView( &pVwRec, NULL );
}


///////////////////////////////////////////////////////////////
//    函 数 名 : oxaGetVar
//    函数功能 :
//    处理过程 :
//    备   注 :
//    作   者 : user
//    时   间 : 2004年6月16日
//    返 回 值 : int
//    参数说明 : const CString strSym,
//               AcGePoint3d &vOut
///////////////////////////////////////////////////////////////
int oxaGetVar(const CString strSym, AcGePoint3d &vOut )
{
   resbuf rbVar ;
   int iRt=acedGetVar(strSym, &rbVar) ;
   if (iRt!=RTNORM)
   {
       return iRt;
   }
   //oxaPrint(&rbVar);
   
if (rbVar.restype==RTPOINT)
   {
       vOut.x=rbVar.resval.rpoint;
       vOut.y=rbVar.resval.rpoint;
   }   
   if (rbVar.restype==RT3DPOINT)
   {
       vOut.x=rbVar.resval.rpoint;
       vOut.y=rbVar.resval.rpoint;
       vOut.z=rbVar.resval.rpoint;
   }   
return RTNORM;
}



/////////////////////////////////////////////////////////////////////////////////
//# DOC.BEGIN
//# 函数名称: oxaGetVar
//# 函数编号: OXA
//# 函数声明:
//# 函数参数: const CString strSym,
//               int &vOut
//# 返回值:   int
//# 函数分类:
//# 函数功能: 获取系统变量, 封装acedGetVar()
//# 注意事项:
//# 涉及的全局变量:
//# 调用的OXARX函数:
//# 函数算法:
//# ACAD版本:R14 R15 R16
//# 配合函数:
//# 类似函数:
//# 替换函数:
//# 现存缺陷:
//# 示例程序:
//# 测试要求:
//# 历史记录: 2003年11月10日 , zjw ,完成
//
//# DOC.END
//////////////////////////////////////////////////////////////////////////

int oxaGetVar(const CString strSym, int &vOut )
{
   resbuf rbVar;
   int iRt=acedGetVar(strSym, &rbVar) ;
   if (iRt!=RTNORM)
   {
       return iRt;
   }
   
if (rbVar.restype==RTLONG)
   {
       vOut=rbVar.resval.rlong;
   }
if (rbVar.restype==RTSHORT)
   {
       vOut=rbVar.resval.rint;
}

return RTNORM;
}

/////////////////////////////////////////////////////////////////////////////////
//# DOC.BEGIN
//# 函数名称: oxaGetVar
//# 函数编号: OXA
//# 函数声明:
//# 函数参数: const CString strSym,
//               double &vOut
//# 返回值:   int
//# 函数分类:
//# 函数功能: 获取系统变量, 封装acedGetVar()
//# 注意事项:
//# 涉及的全局变量:
//# 调用的OXARX函数:
//# 函数算法:
//# ACAD版本:R14 R15 R16
//# 配合函数:
//# 类似函数:
//# 替换函数:
//# 现存缺陷:
//# 示例程序:
//# 测试要求:
//# 历史记录: 2003年11月24日 , zjw ,完成
//
//# DOC.END
int oxaGetVar(const CString strSym, double &vOut )
{
   resbuf rbVar;
   int iRt=acedGetVar(strSym, &rbVar) ;
   if (iRt!=RTNORM)
   {
       return iRt;
   }
   
if (rbVar.restype==RTREAL)
   {
       vOut=rbVar.resval.rreal;
   }   
return RTNORM;
}

/////////////////////////////////////////////////////////////////////////////////
//# DOC.BEGIN
//# 函数名称: oxaGetVar
//# 函数编号: OXA
//# 函数声明:
//# 函数参数: const CString strSym,
//               CString &vOut
//# 返回值:   int
//# 函数分类:
//# 函数功能:获取系统变量, 封装acedGetVar()
//# 注意事项:
//# 涉及的全局变量:
//# 调用的OXARX函数:
//# 函数算法:
//# ACAD版本:R14 R15 R16
//# 配合函数:
//# 类似函数:
//# 替换函数:
//# 现存缺陷:
//# 示例程序:
//# 测试要求:
//# 历史记录: 2003年11月24日 , zjw ,完成
//
//# DOC.END
int oxaGetVar(const CString strSym, CString &vOut )
{
   resbuf rbVar;
   int iRt=acedGetVar(strSym, &rbVar) ;
   if (iRt!=RTNORM)
   {
       return iRt;
   }
   
if (rbVar.restype==RTSTR)
   {
       vOut=rbVar.resval.rstring;
   }   
return RTNORM;
}


// 函数名   : SetCurTextStyle
// 描述   : 设置当前TextStyle
// 返回   : Acad::ErrorStatus
// 参数       : const char* lpStyleName
// 参数       : AcDbDatabase* pDb/* = NULL */
Acad::ErrorStatus SetCurTextStyle(const char* lpStyleName, AcDbDatabase* pDb/* = NULL */)
{
AcDbDatabase* pCurDb = pDb;
if (pCurDb == NULL)
    pCurDb = acdbHostApplicationServices()->workingDatabase();

AcDbTextStyleTableRecordPointer spRecord(lpStyleName, pCurDb, AcDb::kForRead);
Acad::ErrorStatus es = spRecord.openStatus();
if (es == Acad::eOk)
{
    es = pCurDb->setTextstyle(spRecord->objectId());
}
return es;
}


// Function name   : SetCurLayer
// Description   : 设置当前层
// Return type   : Acad::ErrorStatus
// Argument       : const char* lpLayerName
// Argument       : AcDbDatabase* pDb/* = NULL */
Acad::ErrorStatus SetCurLayer(const char* lpLayerName, AcDbDatabase* pDb/* = NULL */)
{
AcDbDatabase* pCurDb = pDb;
if (pCurDb == NULL)
    pCurDb = acdbHostApplicationServices()->workingDatabase();

AcDbLayerTableRecordPointer spRecord(lpLayerName, pCurDb, AcDb::kForRead);
Acad::ErrorStatus es = spRecord.openStatus();
if (es == Acad::eOk)
{
    es = pCurDb->setClayer(spRecord->objectId());
}
return es;
}

//获取属性块中所有属性的字符串值,并且存于链表中

void FEGroups::iterateDictionary()
{
//obtain the GROUP dictionary by looking up "ACAD采用GROUP" in the named object dictionary
//
/* AcDbDictionary *pNamedobj;
acdbHostApplicationServices()->workingDatabase()
    ->getNamedObjectsDictionary(pNamedobj, AcDb::kForRead);

// Get a pointer to the ASDK采用DICT dictionary.
//
AcDbDictionary *pDict;
pNamedobj->getAt("ACAD采用GROUP", (AcDbObject*&)pDict,
    AcDb::kForRead);
pNamedobj->close();
*/
// Get a pointer to the ACAD采用GROUP dictionary
AcDbDictionary *pDict;
acdbHostApplicationServices()->workingDatabase()
    ->getGroupDictionary(pDict, AcDb::kForRead);

// Get an iterator for the ASDK采用DICT dictionary.
//
AcDbDictionaryIterator* pDictIter = pDict->newIterator();
AcDbGroup *pGroup;
char* name;
for (; !pDictIter->done(); pDictIter->next()) {
    // Get the current record, open it for read, and
    // print its name.
    //
    pDictIter->getObject((AcDbObject*&)pGroup,
      AcDb::kForRead);
pGroup->getName(name);
    pGroup->close();
    acutPrintf("\nintval is: %s", name);
}
delete pDictIter;
pDict->close();

}


//检测AutoCAD是否已经运行
void Autocadtest()
{
   // TODO: Add your control notification handler code here
   IAcadApplication m采用autocad;
   IAcadDocuments m采用acaddocs;
   IAcadDocument m采用acaddoc;
   IAcadModelSpace m采用acadmodel;

   LPDISPATCH pDisp;
   LPUNKNOWN pUnk;
   CLSID clsid;
   BeginWaitCursor();
   ::CLSIDFromProgID(L"AutoCAD.Application",&clsid);
   if(::GetActiveObject(clsid,NULL,&pUnk)==S采用OK)
   {
       VERIFY(pUnk->QueryInterface(IID采用IDispatch,(void**) &pDisp)==S采用OK);
       m采用autocad.AttachDispatch(pDisp);
       pUnk->Release();
   }
   else
   {
       if(!m采用autocad.CreateDispatch("AutoCAD.Application"))
       {
         AfxMessageBox("Autocad program not found\n");
         exit(1);
       }
   }
   m采用autocad.SetVisible(true);
   m采用acaddocs.AttachDispatch(m采用autocad.GetDocuments(),true);
   m采用acaddoc.AttachDispatch(m采用acaddocs.Add(vtMissing),true);
   m采用acadmodel.AttachDispatch(m采用acaddoc.GetModelSpace(),true);
   m采用acadmodel.AddCircle(pVal,100);
   
   m采用acadmodel.ReleaseDispatch();
   m采用acaddoc.ReleaseDispatch();
   m采用acaddocs.ReleaseDispatch();
   m采用autocad.ReleaseDispatch();
}


//计算多边形的形心坐标
BOOL GetPolyCentroid(AcDbPolyline * pPline, ads采用point CenPt)
{
unsigned int i, iCount = 0;
AcDbVoidPtrArray curveSegments, regions;
AcGePoint3d LinePt0, LinePt1;
AcGePoint3d origin;
AcGeVector3d xAxis, yAxis;
double perimeter, area, prodInertia;
double momInertia, prinMoments, radiiGyration;
AcGePoint2d centroid;
AcGeVector2d prinAxes;
AcGePoint2d extentsLow, extentsHigh;

if (pPline->isClosed() != Adesk::kTrue) {
ads采用printf("\n折线不封闭, 无法形成正确的区域。");
return FALSE;
}
curveSegments.append((AcDbCurve *) pPline);

if (AcDbRegion::createFromCurves(curveSegments, regions) != Acad::eOk){
ads采用printf("\n创建临时区域对象失败!");
//清除Region, 应第9 贴的指点,即使createFromCurves错误,也应清除之;
iCount = regions.length();
for(i = 0; i < iCount; i++)
delete (AcDbRegion *)regions.at(i);

return FALSE;
}
AcDbRegion * pRegion;
if ((iCount = regions.length()) == 0){
ads采用printf("\n创建临时区域对象为空!");
return FALSE;
}
if (iCount > 1){
// 多个 AcDbRegion , 无法确定应该返回哪一个,干脆返回NULL;
ads采用printf("\n多个区域实体。");
for(i = 0; i < iCount; i++)
delete (AcDbRegion *)regions.at(i);
return FALSE;
}
pRegion = (AcDbRegion *) regions.at(0);

origin.set(0,0,0); //设置原点坐标
xAxis.set(1,0,0); //设置X Y轴,
yAxis.set(0,1,0);

if (pRegion->getAreaProp(
origin, xAxis, yAxis,
perimeter, area, centroid, momInertia, prodInertia, prinMoments, prinAxes, radiiGyration,
extentsLow, extentsHigh) != Acad::eOk ){
ads采用printf("\n区域面积: %.3f, 周长:%.3f", area, perimeter);
ads采用printf("\n获取区域对象属性失败!");
delete pRegion;
return FALSE;
}
XYZ采用POINT(CenPt, centroid.x, centroid.y, 0); //得到形心坐标
ads采用printf("\n区域面积: %.3f, 周长:%.3f", area, perimeter);
pRegion->close();
delete pRegion;

return TRUE;
}

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;

}

//添加扩展数据
//实体添加扩展数据(字符串)
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;

wnd = adsw采用acadMainWnd();
if(!wnd) return;

COPYDATASTRUCT cmddata;
cp = VK采用ESCAPE;
cp = VK采用ESCAPE;
cp = 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,EndPoint,0);
Start = AcGePoint3d(StartPoint,StartPoint,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);
AcDb3dSolid *p3dobj = new AcDb3dSolid;
es = p3dobj->revolve(pregion1,pt,ver,angle);
if (es != Acad::eOk)
{
acutPrintf("建立回转体失败!请检查回转轴和基准点是否正确!");
}
pBlockTableRecord->appendAcDbEntity(tm,p3dobj);
p3dobj->close();
delete pregion1;
}






在ObjectARX 实现 Command 的 *Cancel* 功能: (类似 AutoLISP 中的 ^C)
acedCommand(0); // 就可以了
例如:
acedCommand (RTSTR, "dim1", RTSTR, "leader", RTSTR, "0,0", RTSTR, "10,10", 0);
acedCommand (0);



//复制对象
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());
}

}





void selObj()
{
#ifdef OARXWIZDEBUG
acutPrintf ("\nOARXWIZDEBUG - caditdellayObjdellayObj() called.");
#endif // OARXWIZDEBUG
ads采用name ents;
struct resbuf *rb;
AcDbEntity * pEnt;
AcDbText *sText;
AcDbObjectId objId;
ads采用name ent;
rb=acutNewRb(AcDb::kDxfLayerName);
rb->rbnext=acutNewRb(AcDb::kDxfRegAppName);
rb->restype=8;
rb->resval.rstring="001";
rb=rb->rbnext;
rb->restype=1001;
rb->resval.rstring ="ll";
rb->rbnext=NULL;
acedSSGet("X",NULL,NULL,rb,ents);
long entNums=0;

acedSSLength(ents,&entNums);
if (entNums!= 0)
{


for (long a = 0; a < entNums ; a ++)
{

acedSSName(ents,a,ent);

// ads采用entdel(ent);
acdbGetObjectId(objId, ent);
acdbOpenObject(pEnt, objId,AcDb::kForRead);
if(pEnt->isKindOf(AcDbText::desc()))
{
pEnt->close();
acdbOpenObject(sText, objId,AcDb::kForWrite);
ads采用printf(sText->textString());
if (strcmp(sText->textString(),"text")==0)
{
sText->setHeight(100);
sText->setTextString("HELLO");
sText->setColorIndex(5);
}
sText->close();
}
else
{
pEnt->close();
}
}
}

acedSSFree(ents);
acutRelRb(rb);
// TODO: Implement the command

}


//转换AcDbCurve到AcGeCurve3d
Acad::ErrorStatus XdDbUtils::convertDbCurveToGeCurve(AcDbCurve *pDbCurve,AcGeCurve3d *&pGeCurve)
{
   pGeCurve=NULL;
   if (pDbCurve->isKindOf(AcDbLine::desc()))
   {
      AcDbLine *pL=(AcDbLine *)pDbCurve;
      AcGeLineSeg3d *pGL=new AcGeLineSeg3d;
      pGL->set(pL->startPoint(),pL->endPoint());
      pGeCurve=(AcGeCurve3d *)pGL;
   }
   else if (pDbCurve->isKindOf(AcDbArc::desc()))
   {
      AcDbArc *pArc=(AcDbArc *)pDbCurve;
      double ans,ane;
      ans=pArc->startAngle();
      ane=pArc->endAngle();
      AcGeCircArc3d *pGArc=new AcGeCircArc3d;
      pGArc->setCenter(pArc->center());
      pGArc->setRadius(pArc->radius());
      pGArc->setAngles(ans,ane);
      pGeCurve=(AcGeCurve3d *)pGArc;
   }
   else if (pDbCurve->isKindOf(AcDbCircle::desc()))
   {
      AcDbCircle *pCir=(AcDbCircle *)pDbCurve;
      AcGeCircArc3d * pGCir=new AcGeCircArc3d;
      pGCir->setCenter(pCir->center());
      pGCir->setRadius(pCir->radius());
      pGeCurve=(AcGeCurve3d *)pGCir;
   }
   else if (pDbCurve->isKindOf(AcDbEllipse::desc()))
   {
      AcDbEllipse *pEli=(AcDbEllipse *)pDbCurve;
      AcGePoint3d pt1,center=pEli->center();
      AcGeEllipArc3d *pGEli=new AcGeEllipArc3d;
      pGEli->setCenter(center);
      pGEli->setAxes(pEli->majorAxis(),pEli->minorAxis());
      pEli->getClosestPointTo(center,pt1,Adesk::kTrue);
      pGEli->setMajorRadius(pt1.distanceTo(center)/pEli->radiusRatio());
      pGEli->setMinorRadius(pt1.distanceTo(center));
      double endang=pEli->endAngle(),startang=pEli->startAngle();
      if (startang>endang){
      endang+=2*PI;
      }
      pGEli->setAngles(endang,startang);
      pGeCurve=(AcGeCurve3d *)pGEli;
   }
   else if (pDbCurve->isKindOf(AcDbSpline::desc()))
   {
      AcDbSpline *pSL=(AcDbSpline *)pDbCurve;
      if (!pSL)
      return Acad::eNotImplemented;
      if (pSL->isNull()==Adesk::kTrue)
      return Acad::eNotImplemented;
      
      int degree;
      Adesk::Boolean rational;
      Adesk::Boolean closed;
      Adesk::Boolean periodic;
      AcGePoint3dArray controlPoints;
      AcGeDoubleArray knots;
      AcGeDoubleArray weights;
      double controlPtTol;
      double knotTol;
      AcGeTol tol;
      Acad::ErrorStatus es;
      es=pSL->getNurbsData(degree,rational,closed,periodic,controlPoints,knots,weights,
      controlPtTol,knotTol);
      if (es!=Acad::eOk)
      return Acad::eNotImplemented;
      
      if (rational==Adesk::kTrue)
      {
      AcGeNurbCurve3d *pNurb=new AcGeNurbCurve3d(degree,knots,controlPoints,weights,periodic);
      if (closed==Adesk::kTrue)
         pNurb->makeClosed();
      if (pSL->hasFitData()==Adesk::kTrue)
      {
         AcGePoint3dArray fitPoints;
         double fitTolerance;
         Adesk::Boolean tangentsExist;
         AcGeVector3d startTangent;
         AcGeVector3d endTangent;
         pSL->getFitData(fitPoints,degree,fitTolerance,tangentsExist,startTangent,endTangent);
         tol.setEqualPoint(fitTolerance);
         if (tangentsExist==Adesk::kTrue)
            pNurb->setFitData(fitPoints,startTangent,endTangent,tol);
         else
            pNurb->setFitData(degree,fitPoints,tol);
         
      }
      pGeCurve=(AcGeCurve3d *)pNurb;
      }
      else
      {
      AcGeNurbCurve3d *pNurb=new AcGeNurbCurve3d(degree,knots,controlPoints,periodic);
      if (closed==Adesk::kTrue)
         pNurb->makeClosed();
      if (pSL->hasFitData()==Adesk::kTrue)
      {
         AcGePoint3dArray fitPoints;
         double fitTolerance;
         Adesk::Boolean tangentsExist;
         AcGeVector3d startTangent;
         AcGeVector3d endTangent;
         pSL->getFitData(fitPoints,degree,fitTolerance,tangentsExist,startTangent,endTangent);
         tol.setEqualPoint(fitTolerance);
         if (tangentsExist==Adesk::kTrue)
            pNurb->setFitData(fitPoints,startTangent,endTangent,tol);
         else
            pNurb->setFitData(degree,fitPoints,tol);
         
      }
      pGeCurve=(AcGeCurve3d *)pNurb;
      }
   }
   else if ((pDbCurve->isKindOf(AcDb2dPolyline::desc()))||
      (pDbCurve->isKindOf(AcDbPolyline::desc())))
   {
      int type=0;
      AcDbPolyline *pPoly;
      if (pDbCurve->isKindOf(AcDb2dPolyline::desc()))
      {
      AcDb2dPolyline *p2L=(AcDb2dPolyline *)pDbCurve;
      XdDbUtils::Poly2dToLWPoly(p2L,pPoly);
      type=1;
      }
      else
      pPoly=(AcDbPolyline *)pDbCurve;
      XdDbUtils::convertPolylineToGeCurve(pPoly,pGeCurve);
      if (type)
      delete pPoly;
   }
   return (pGeCurve)?Acad::eOk:Acad::eNotImplemented;
}





// convert AcDbLine to AcGeLineSeg3d
AcGeLineSeg3d* LineDb2GE(AcDbLine* pDbLine)
{
   return(new AcGeLineSeg3d(pDbLine->startPoint(), pDbLine->endPoint()));
}

// convert AcDbArc to AcGeCircArc3d
AcGeCircArc3d* ArcDb2Ge( AcDbArc* pDbArc)
{
   return(new AcGeCircArc3d(
      pDbArc->center(),
      pDbArc->normal(),
      pDbArc->normal().perpVector(),
      pDbArc->radius(),
      pDbArc->startAngle(),
      pDbArc->endAngle()));
}

// convert AcDbCircle to AcGeCircArc3d
AcGeCircArc3d* CircleDb2Ge(AcDbCircle* pDbCircle)
{
   return(new AcGeCircArc3d(
      pDbCircle->center(),
      pDbCircle->normal(),
      pDbCircle->radius()));

}

// convert AcDbSpline to AcGeNurbCurve3d
AcGeNurbCurve3d* SplineDb2Ge(AcDbSpline* pDbSpline)
{
   AcGeNurbCurve3d* pGeSpline;
   AcGePoint3dArray fitPoints;
   int degree;
   double fitTolerance;
   Adesk::Boolean tangentsExist;
   AcGeVector3d startTangent, endTangent;
   AcGeTol tol;

   Adesk::Boolean rational, closed, periodic;
   AcGePoint3dArray controlPoints;
   AcGeDoubleArray knots, weights;
   double controlPtTol, knotTol;

   if (pDbSpline->hasFitData()) {
      pDbSpline->getFitData(fitPoints, degree, fitTolerance,
      tangentsExist,startTangent, endTangent);
      
      tol.setEqualPoint(fitTolerance);
      pGeSpline=new AcGeNurbCurve3d(fitPoints, startTangent,
      endTangent, tangentsExist, tangentsExist,tol);      
   }else{
      pDbSpline->getNurbsData(degree, rational, closed, periodic,
      controlPoints, knots, weights, controlPtTol, knotTol);

      pGeSpline=new AcGeNurbCurve3d(degree, knots, controlPoints,
      weights, periodic);
      if (closed==Adesk::kTrue)
      pGeSpline->makeClosed();

   };
   return(pGeSpline);

}

// convert AcDbEllipse to AcGeEllipArc3d
AcGeEllipArc3d* EllipseDb2Ge(AcDbEllipse* pDbEllise)
{
   return(new AcGeEllipArc3d(
      pDbEllise->center(),
      pDbEllise->majorAxis(),
      pDbEllise->minorAxis(),
      pDbEllise->majorAxis().length(),
      pDbEllise->minorAxis().length(),
      pDbEllise->startAngle(),
      pDbEllise->endAngle()));
}

// - CGDgetOsnapPoint.getOsnapPoint command (do not rename)
static void CGDgetOsnapPointgetOsnapPoint(void)
{
// Add your code for command CGDgetOsnapPoint.getOsnapPoint here
   AcDbDatabase *pDb = acdbHostApplicationServices()->workingDatabase();
   AcDbBlockTable *pBT = NULL;
   AcDbBlockTableRecord *pBTR = NULL;
   Acad::ErrorStatus es;
   es = pDb->getSymbolTable (pBT,AcDb::kForRead );
   if(es != Acad::eOk)
      {
      acutPrintf("get block table failure,exit\n");
      return ;
      }
   es = pBT->getAt (ACDB采用MODEL采用SPACE,pBTR,AcDb::kForWrite );
   if(es != Acad::eOk)
      {
      acutPrintf("get block table record failure,exit\n");
      return ;
      }

   AcGePoint3d pnt1,pnt2,pnt;

   pnt1.set(8.0, 7.0, 0.0);
   pnt2.set(7.0, 7.0, 0.0);
   pnt.set(9.0,8.0,0.0);

   AcDbLine *pLine = new AcDbLine(pnt1,pnt2);
   AcDbObjectId idLine = AcDbObjectId::kNull ;
   pBTR->appendAcDbEntity (idLine,pLine);

   AcGePoint3dArray snapPoints;

   AcDbIntArray geomIds;

   es = pLine->getOsnapPoints(AcDb::kOsModePerp, 0, pnt, pnt, AcGeMatrix3d::kIdentity, snapPoints, geomIds);

   for(int i = 0;i<snapPoints.length();i++)
      {
      acutPrintf("/nthe point is %.2f,%f.2,%.2f/n",snapPoints.x ,snapPoints.y,snapPoints.z );
      }

   pLine->close ();
   pBTR->close();
   pBT->close();


///END OF COMMAND FUNCTION
}





页: [1]
查看完整版本: objectarx自定义类函数