ObjectARX代码片段二

转载自网络

一  在ARX中禁用AutoCAD的某个命令
 以LINE命令为例,在程序中加入下面的一句即可禁用LINE命令:

acedCommand(RTSTR, "undefine", RTSTR, "line", RTNONE);

下面的语句则可恢复LINE命令的定义:

acedCommand(RTSTR, "redefine", RTSTR, "line", RTNONE);

 

二 在对话框中预览DWG文件

复制代码
//使用acdbDisplayPreviewFromDwg函数,具体的方法为:
char fileName[100];
strcpy(fileName, "C:\\test.dwg");
bool es;
HWND pWnd;
CFrameWnd *pFrame = (CFrameWnd*)GetDlgItem(IDC_PICTURE); 

es = acdbDisplayPreviewFromDwg(fileName, pFrame->m_hWnd);
//上面的代码将在一个Picture控件中显示指定的图形。
//另外,需要包含“dbmain.h”头文件。 
复制代码

 

三 通过ARX更改AutoCAD窗口的标题名称

CMDIFrameWnd *pp; 
pp=acedGetAcadFrame(); 
pp->SetWindowText ("yourName"); 
pp->UpdateWindow (); 

 

四 获得当前数据库

//在ARX编程中,经常需要使用当前数据库,例如需要获得当前图形中设置的文字样式、标注样式等。
//要获得当前数据库,都可以直接使用下面的方法:
AcDbTextStyleTable *pTextStyleTAble;
AcDbObjectId textstyleId;
textstyleId=acdbHostApplicationServices()->workingDatabase()->textstyle();
//如果用acadCurDwg来代替acdbHostApplicationServices()->workingDatabase(),也能得到同样的结果。

 

五 将一个图插入另一个图(两种方法)
在实践中常常要将外部的图形文件source.dwg中插入到另外一个图形中或者当前图形中.以插入到当前图形dest.dwg为例. 
为了将一个source.dwg插入dest.dwg中,首先要找到source.dwg这个文件. 我们可以使用各种基本的技术和相应的规则从你的机器上或PDM数据库中检索到你要插入的source.dwg, 返回来一个字符窜sFileName代表整个文件及其路径. 
然后创建一个空的数据库pNewDb读入source.dwg. 

复制代码
AcDbDatabase *pNewDb =new AcDbDatabase(Adesk::kFalse); 
//在这里一定要用Adesk::kFalse 
acDocManager->lockDocument(acDocManager->curDocument()) ;//如果确定你不需要LockDocument, 这一行可以不要 

es=pNewDb->readDwgFile(sFileName , _SH_DENYNO,false); 
if (es!=Acad::eOk) 
{ 
    acutPrintf("\nThe file %s cannot be opend",sFileName); 
    return; 
} 

//这样,source.dwg以经用pNewDb来表示了. 我们用pDb来表示当前数据库 
AcDbDatabase *pDb; 
pDb =acdbHostApplicationServices ()->workingDatabase () ; 
复制代码

现在,我们用Insert来插入数据库. Insert有两种用法,一种是直接insert, source.dwg中的图元实体被分散地插入pDb中 
pDb->insert( AcGeMatrix3d::kIdentity, pNewDb );//这里假定不对source.dwg做比例和转角的变换. 如果我们在这里结束程序,我们能看到source.dwg已经被插入,但不是一个图块. 

另外一种插入法是要求插入后source.dwg成为一个图块,图块的attribute也要从source.dwg中得到.这种方法要做大量的工作.首先运行insert() 

复制代码
CString pBlockName=”TestBlock”; 
AcDbObjectId blockId; 
if((es=pDb->insert(blockId, pBlockName,pNewDb, true))==Acad::eOk) 
{ 
    acutPrintf("\ninsert ok\n"); 
} 
else 
{ 
    AfxMessageBox("Insert failed"); 
    delete pNewDb; 
    return; 
} 

//这里blcokId是insert运行后产生的,它代表的是一个块表记录AcDbBlockRecord的ID. pBlockName是记录名,要在insert运行前设定其值. 
复制代码

如果我们在这里结束程序,我们看不到任何东西,因为source并没有真正被插入.我们还要做一些事,首先是创建一个AcDbBlockReference, 并将它指向blockId所代表的AcDbBlockRecord, 然后将这个AcDbBlockReference加入pDb所代表的图形数据库中. 

复制代码
AcDbBlockReference *pBlkRef = new AcDbBlockReference; 
pBlkRef->setBlockTableRecord(blockId);//指向blockId; 
pBlkRef->setPosition(Pt);//设定位置 
pBlkRef->setRotation(Angle);//设定转角 
pBlkRef->setScaleFactors( XrefScale);//设定放大比例 

AcDbBlockTable *pBlockTable; 
pDb->getSymbolTable(pBlockTable, AcDb::kForRead); 

AcDbBlockTableRecord *pBlockTableRecord; 
pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord, AcDb::kForWrite); 
pBlockTable->close(); 
AcDbObjectId newEntId; 
pBlockTableRecord->appendAcDbEntity(newEntId, pBlkRef); 
pBlockTableRecord->close(); 
复制代码

如果我们在这里结束程序,我们将看到当前图形中source.dwg已经被作为图块插入.但是图块中没有source.dwg所定义的Attibute. 因此我们还要做工作.后面的事情就简单了. 

复制代码
AcDbBlockTableRecord *pBlockDef; 
acdbOpenObject(pBlockDef, blockId, AcDb::kForRead); 

AcDbBlockTableRecordIterator *pIterator; 
pBlockDef->newIterator(pIterator); 
AcGePoint3d basePoint; 
AcDbEntity *pEnt; 
AcDbAttributeDefinition *pAttdef; 
for (pIterator->start(); !pIterator->done(); 
    pIterator->step())//将source.dwg中所有的Attibute进行遍历 
{ 
    pIterator->getEntity(pEnt, AcDb::kForRead); 
    pAttdef = AcDbAttributeDefinition::cast(pEnt); 
    if (pAttdef != NULL && !pAttdef->isConstant()) { 
        AcDbAttribute *pAtt = new AcDbAttribute(); 
        pAtt->setPropertiesFrom(pAttdef); 
        pAtt->setInvisible(pAttdef->isInvisible()); 
        basePoint = pAttdef->position(); 
        basePoint += pBlkRef->position().asVector(); 
        pAtt->setPosition(basePoint); 
        pAtt->setHeight(pAttdef->height()); 
        pAtt->setRotation(pAttdef->rotation()); 
        pAtt->setTag("Tag"); 
        pAtt->setFieldLength(25); 
        char *pStr = pAttdef->tag(); 
        pAtt->setTag(pStr); 
        acutDelString(pStr); 
        pAtt->setFieldLength(pAttdef->fieldLength()); 
        pAtt->setTextString("-"); 

        AcDbObjectId attId; 

        pBlkRef->appendAttribute(attId, pAtt); 
        pAtt->close(); 
    } 
    pEnt->close(); // use pEnt... pAttdef might be NULL 
} 
delete pIterator;
复制代码

 

六 在ARX打开文件
在AutoCAD中打开图形,并且显示在图形窗口中,可以使用acedSyncFileOpen()函数。需要注意的是,这个函数只能在单文档模式中工作,

用户可以在AutoCAD“选项”对话框的“系统”选项卡中进行设置,或者在主函数中添加下面的语句:

acrxDynamicLinker->registerAppNotMDIAware(pkt);

具体的函数如下:

复制代码
//加载模板文件
void LoadTemplate()
{
    char fname[50];
    strcpy(fname,"E:\\TMCAD\\TMCADtukuang\\A3.DWG");

#ifndef _ACAD2000
    Acad::ErrorStatuses;  
    es = acedSyncFileOpen(fname);
#else 
    acDocManager->appContextOpenDocument(fname);
#endif
}
复制代码

如果在多文档模式下,下面的方法可以在执行时弹出“选择文件”对话框,用户选择所要打开的文件后,在图形窗口中显示该图形。

void ZffOPENOpenDwg()
{
 // 使用“选择文件”对话框选择所要打开的文件
 acDocManager->openDocument();
}

下面的方法则打开指定位置的DWG文件。

复制代码
void OpenDoc( void *pData)
{
    AcApDocument* pDoc = acDocManager->curDocument();
    if (acDocManager->isApplicationContext()) 
    {
        acDocManager->appContextOpenDocument((const char *)pData);
    } 
    else
    {
        acutPrintf("\nERROR To Open Doc!\n");
    }
}
复制代码
复制代码
// This is command 'OPEN1'
void ZffOPENopen1()
{
    // 直接打开系统中存在的某个图形文件G:\AutoCAD图形\wen2.dwg
    static char pData[] = "G:\\AutoCAD图形\\wen2.dwg"; 
    acDocManager->executeInApplicationContext(OpenDoc, (void *)pData);
} 
复制代码

设置当前的layer和textstyle

复制代码
//  Function name    : SetCurTextStyle
//  Description      : 设置当前TextStyle
//  Return type      : Acad::ErrorStatus 
//  Argument         : const char* lpStyleName
//  Argument         : 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;
} 
复制代码

 

复制代码
 1 //功能描述:选择指定图层上的所有实体!
 2 
 3     // ************************************************************************
 4     // 函数名称:selectEntityInLayer
 5     // 函数类型:Acad::ErrorStatus 
 6     // 返回值:  正常:Acad::eOk
 7     // 功能描述:选择指定图层上的所有实体!
 8     // 函数作者:Darcy
 9     // 创建日期:200X-XX-XX
10     // 参数列表:
11     // 变量名:nLayerName     变量类型:const char*           变量说明:(输入)图层名
12     // 变量名:nIDs           变量类型:AcDbObjectIdArray&    变量说明:(输出)图层中实体的对象标识符集合
13     // ************************************************************************ 
14     Acad::ErrorStatus selectEntityInLayer( const   char *  nLayerName,AcDbObjectIdArray &  nIDs)
15 {
16     Acad::ErrorStatus es  =  Acad::eOk;
17 
18     ads_name ents;
19     struct  resbuf  * rb;
20     rb = acutNewRb(AcDb::kDxfLayerName);
21     rb -> restype = 8 ;
22     rb -> resval.rstring = ( char * )nLayerName;
23     rb -> rbnext = NULL;
24     acedSSGet( " X " ,NULL,NULL,rb,ents);
25     long  entNums = 0 ;
26     acedSSLength(ents, & entNums);
27     if  (entNums  ==   0 )
28         es  =  Acad::eInvalidInput;
29     else 
30     {
31         for  ( long  a  =   0 ; a  <  entNums ; a  ++ )
32         {
33             AcDbObjectId  objId;
34             ads_name      ent;
35             acedSSName(ents,a,ent);
36             acdbGetObjectId(objId, ent);
37             nIDs.append(objId);
38         } 
39     } 
40     acedSSFree(ents);
41     acutRelRb(rb);
42 
43     return  es;
44 } 
复制代码

另一种实现

复制代码
 1 //************************************************************************
 2 //函数名称:selectEntityInLayer
 3 //函数类型:Acad::ErrorStatus 
 4 //返回值:
 5 //功能描述:选择指定层上的实体,得到其对象属性标识符!
 6 //函数作者:Darcy
 7 //创建日期:200X-XX-XX
 8 //参数列表:
 9 //变量名:nLayerName      变量类型:CString               变量说明:
10 //变量名:nIDs            变量类型:AcDbObjectIdArray&    变量说明:
11 //变量名:nModelSpace     变量类型:bool                  变量说明:
12 //************************************************************************
13 Acad::ErrorStatus    selectEntityInLayer(
14                                          CString nLayerName,
15                                          AcDbObjectIdArray& nIDs,
16                                          bool nModelSpace
17                                          )
18 {
19     Acad::ErrorStatus es=Acad::eOk; 
20 
21     AcDbBlockTable*        pBlockTable=NULL;
22     AcDbBlockTableRecord*  pSpaceRecord=NULL;
23     if (acdbHostApplicationServices()->workingDatabase()==NULL)
24         return Acad::eNoDatabase;
25     if ((es = acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlockTable, AcDb::kForRead))==Acad::eOk)
26     {
27         char entryName[13];
28         if (nModelSpace)
29             strcpy(entryName,ACDB_MODEL_SPACE);
30         else
31             strcpy(entryName,ACDB_PAPER_SPACE);
32         //Get the Model or Paper Space record and open it for read:  
33         if ((es = pBlockTable->getAt((const char*)entryName, pSpaceRecord, AcDb::kForRead))==Acad::eOk)
34         {
35                 AcDbBlockTableRecordIterator* pIter;
36                 if (pSpaceRecord->newIterator(pIter)==Acad::eOk)
37                 {
38                     for (pIter->start();!pIter->done();pIter->step())
39                     {
40                         char *name=NULL;
41                         AcDbEntity* pEntity;
42                         if (pIter->getEntity(pEntity,AcDb::kForRead)==Acad::eOk)
43                         {
44                             name=pEntity->layer();
45                             if (nLayerName.CompareNoCase(name)==0)
46                                 nIDs.append(pEntity->objectId());
47 
48                             pEntity->close();
49                             acutDelString(name); 
50                         }
51                     }
52                     delete pIter;
53                 }            
54                 pSpaceRecord->close();
55         }   
56         pBlockTable->close();
57     }
58 
59     return es;
60 }
复制代码

 

生成新组

复制代码
 1 //生成新组(sGroupName)
 2 //追加数组中所有实体到该组中
 3 //组名字 ,   Id数组
 4 int createGroup(CString sGroupName,
 5     const AcDbObjectIdArray *idArr)
 6 {
 7     AcDbGroup       *pGroup = new AcDbGroup((LPSTR)(LPCTSTR)sGroupName);
 8     AcDbObjectId     groupObjectId;
 9     AcDbDictionary  *pGroupDict = NULL;
10 
11     acdbHostApplicationServices()->workingDatabase()
12         ->getGroupDictionary(pGroupDict, AcDb::kForWrite);
13     pGroupDict->setAt(sGroupName, pGroup, groupObjectId);
14     pGroupDict->close();
15     pGroup->close();
16     acdbOpenObject(pGroup, groupObjectId, AcDb::kForWrite);
17     for (int i = 0; i < idArr->length(); i++)
18     {
19         groupObjectId = idArr->at(i);
20         pGroup->append(groupObjectId);   
21     }
22     pGroup->close();
23     return TRUE;
24 }
复制代码

另一个

复制代码
 1 Acad::ErrorStatus CDrawFunction::createGroup(CString groupname,AcDbObjectIdArray IdArray)
 2 {
 3     Acad::ErrorStatus es=Acad::eOk;
 4     AcDbDictionary *pGroupDict=NULL;
 5     AcDbGroup *pGroup=NULL;
 6     if((es=acdbHostApplicationServices()->workingDatabase()->getGroupDictionary(pGroupDict,AcDb::kForWrite))!=Acad::eOk)
 7     {
 8         return es;
 9     }
10     AcDbObjectId groupId;
11     es=pGroupDict->getAt(groupname,groupId);
12     if(es==Acad::eInvalidKey)
13     {
14         acutPrintf("\n输入的词典名无效!");
15         pGroupDict->close();
16         return es;
17     }
18     else if(es==Acad::eKeyNotFound)
19     {
20         pGroup=new AcDbGroup("GroupDiscription");
21         if((es=pGroupDict->setAt(groupname,pGroup,groupId))!=Acad::eOk)
22         {
23             pGroup->close();pGroupDict->close();return es;
24         }
25     }
26     else if(es==Acad::eOk )
27     {
28         if((es=acdbOpenObject(pGroup,groupId,AcDb::kForWrite))!=Acad::eOk)
29         {
30             pGroupDict->close();return es;
31         }
32     }
33     for(int i=0;i pGroup->append(IdArray[i]);
34         pGroup->setSelectable(FALSE);
35         pGroupDict->close();
36     pGroup->close();
37     return es;
38 }
复制代码

 

旋转整个group

复制代码
 1 void CMyDatabase::rotationGroup(CString strGroupName ,CReiPoint ptRotation,double rotationAngle) 
 2 {
 3     AcGePoint3d pt;
 4     AcDbDictionary *pGroupDict;
 5     acdbCurDwg()->getGroupDictionary(pGroupDict,AcDb::kForWrite);
 6     AcDbObjectId groupId;
 7     AcDbGroup *pGroup;
 8     pt.x=ptRotation.x;
 9     pt.y=ptRotation.y;
10     pt.z=ptRotation.z;
11     if(pGroupDict->getAt(strGroupName,groupId)==Acad::eOk)
12         acdbOpenObject(pGroup,groupId,AcDb::kForWrite);
13     else
14     {
15         pGroupDict->close();
16         return;
17     }
18     pGroupDict->close();
19     AcDbGroupIterator* pIter=pGroup->newIterator();
20     AcDbEntity* pEnt;
21     AcDbObjectId objId;
22     pIter=pGroup->newIterator();
23     for(;!pIter->done();pIter->next())
24     {
25         objId=pIter->objectId();
26         acdbOpenAcDbEntity(pEnt,objId,AcDb::kForWrite);
27         rotationEntity(pEnt,pt,rotationAngle);
28         pEnt->close();
29     }
30     delete pIter;
31     pGroup->close();
32 }
复制代码

 

新建一个图层.

复制代码
 1 //==========================================================
 2 //功能:新建一个图层
 3 //参数:LayerName -- 图层名,LayerColor -- 颜色名
 4 //返回:图层ID
 5 //==========================================================
 6 AcDbObjectId CreateNewLayer(CString LayerName, AcCmColor LayerColor)
 7 {
 8     // 获得当前图形数据库的符号表
 9     AcDbLayerTable *pLayerTable;
10     acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pLayerTable,
11         AcDb::kForWrite);
12     // 生成新的图层表记录
13     AcDbLayerTableRecord *pLayerTableRecord = new AcDbLayerTableRecord;
14     pLayerTableRecord->setName(LayerName);        // 设置图层名
15     pLayerTableRecord->setColor(LayerColor);    // 设置图层颜色
16     AcDbObjectId layerId;
17     pLayerTable->add(layerId,pLayerTableRecord);
18     // 关闭图层表和图层表记录
19     pLayerTable->close();
20     pLayerTableRecord->close();
21     return layerId;
22 }
复制代码

 

复制代码
 1 //==========================================================
 2 //功能:在指定图层上新建一条直线
 3 //参数:见注释
 4 //返回:直线ID
 5 //==========================================================
 6 AcDbObjectId CreateLine( double x1,double y1,double z1,  // 起点坐标
 7     double x2,double y2,double z2,  // 终点坐标
 8     AcDbObjectId layer)                   // 直线所在图层
 9 {
10     AcGePoint3d StartPt(x1,y1,z1);    // 起点
11     AcGePoint3d EndPt(x2,y2,z2);    // 终点
12     AcDbLine *pLine = new AcDbLine(StartPt,EndPt);
13     // 获得当前图形数据库的符号表
14     AcDbBlockTable *pBlockTable;
15     acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pBlockTable,
16         AcDb::kForRead);
17     // 获得符号表中的模型空间块表记录指针,用于添加对象
18     AcDbBlockTableRecord *pBlockTableRecord;
19     pBlockTable->getAt(ACDB_MODEL_SPACE,pBlockTableRecord,AcDb::kForWrite);
20     pBlockTable->close();
21     // 将直线添加到模型空间块表记录中
22     AcDbObjectId lineId;
23     pLine->setLayer(layer,Adesk::kTrue);    // 设置直线所在图层
24     pBlockTableRecord->appendAcDbEntity(lineId,pLine);
25     // 关闭块表记录指针和直线指针
26     pBlockTableRecord->close();
27     pLine->close();
28     // 返回直线ID号
29     return lineId;
30 }
复制代码

 

已知一段弧的起点和终点以及其凸度,求其圆心

复制代码
 1 int getCenter(ads_point startPoint,ads_point endPoint,double bulge,ads_point& center)
 2 {
 3     if (bulge==0.0)
 4     {
 5         ads_point_set(startPoint,center);
 6         return 0;
 7     }
 8     ads_point startPt,endPt;
 9     if (bulge<0.0)
10     {
11         ads_point_set(endPoint,startPt);
12         ads_point_set(startPoint,endPt);
13         bulge=bulge*(-1.0);
14     }
15     else
16     {
17         ads_point_set(startPoint,startPt);
18         ads_point_set(endPoint,endPt);
19     }
20     double distStartToEnd,distX,distY,radius;
21     distStartToEnd=ads_distance(startPt,endPt);
22     distX=distStartToEnd/2.0;
23     distY=bulge*distX;
24     radius=((distX*distX)+(distY*distY))/(2.0*distY);
25 
26     double tmpAng;
27     ads_point tmpPt;
28 
29     tmpAng=ads_angle(startPt,endPt);
30     ads_polar(startPt,tmpAng,distX,tmpPt);
31     ads_polar(tmpPt,(tmpAng+(_PI/2.0)),(radius-distY),center);
32     return 1;
33 
34 }; 
复制代码

 

创建填充

复制代码
 1 AcDbObjectId CreateHatch(
 2     AcDbObjectId dbOId,
 3     char cLayer[], 
 4     char cPattern[] = "SOLID", 
 5     int nColor = 256, 
 6     double dAngle = 0.0, 
 7     double dScale = 1.0, 
 8     AcDbDatabase * pDbDatab = acdbHostApplicationServices()->workingDatabase())
 9 {
10     AcCmColor CmC;
11     AcDbObjectId DbOId;
12     AcDbObjectIdArray DbOIdA(0, 2);
13     AcDbBlockTable * pDbBT;
14     AcDbBlockTableRecord * pDbBTR;
15     AcGeVector3d normal(0.0, 0.0, 1.0);
16 
17     DbOIdA.append(dbOId);
18 
19     AcDbHatch* pDbHat = new AcDbHatch();
20 
21     pDbHat->setDatabaseDefaults();
22 
23     pDbHat->setAssociative(Adesk::kTrue); // BUG: doesn't do squat! have to set the reactor yourself to get associativity!
24 
25     pDbHat->appendLoop(AcDbHatch::kExternal, DbOIdA);
26 
27     pDbHat->setPatternScale(dScale);
28 
29     pDbHat->setPatternAngle(dAngle);
30 
31     pDbHat->setPattern(AcDbHatch::kPreDefined, cPattern);
32 
33     pDbHat->setNormal(normal);
34 
35     pDbHat->evaluateHatch(); // crucial call or nothing gets displayed!
36 
37     pDbDatab->getSymbolTable(pDbBT, AcDb::kForRead);
38 
39     pDbBT->getAt(ACDB_MODEL_SPACE, pDbBTR, AcDb::kForWrite);
40 
41     pDbBTR->appendAcDbEntity(DbOId, pDbHat);
42 
43     pDbHat->setLayer(cLayer);
44 
45     CmC.setColorIndex(nColor);
46 
47     ((AcDbEntity *)pDbHat)->setColor(CmC);
48 
49     pDbBT->close();
50 
51     pDbBTR->close();
52 
53     pDbHat->close();
54 
55     return DbOId;
56 
57 }
复制代码

 

复制代码
 1 //************************************************************************
 2 //函数名称:getPointAtDistInGeCurve
 3 //函数类型:Acad::ErrorStatus 
 4 //返回值:
 5 //功能描述:返回曲线上距起点某距离值处的点。
 6 //函数作者:Darcy
 7 //创建日期:2003-XX-XX
 8 //参数列表:
 9 //变量名:pGeCurve 变量类型:const AcGeCurve3d * 变量说明:
10 //变量名:dist 变量类型:double 变量说明:
11 //变量名:point 变量类型:AcGePoint3d& 变量说明:
12 //备 注:
13 //************************************************************************
14 Acad::ErrorStatus getPointAtDistInGeCurve(const AcGeCurve3d * pGeCurve,double dist,AcGePoint3d& point)
15 {
16     Acad::ErrorStatus es = Acad::eOk;
17 
18     if ( pGeCurve != NULL )
19     {
20         AcGePoint3d spt;
21         double pa=0.,datumParam=0.;
22         //距离从起点起算!
23         Adesk::Boolean posParamDir=Adesk::kTrue;
24 
25         pGeCurve->hasStartPoint(spt); 
26 
27         datumParam = pGeCurve->paramOf(spt);;
28 
29         pa = pGeCurve->paramAtLength(
30             datumParam,
31             dist,
32             posParamDir
33             );
34 
35         point=pGeCurve->evalPoint(pa);
36     }
37     else
38         es = Acad::eInvalidInput;
39 
40     return es;
41 } 
复制代码

 

判断点是否在圆弧上

复制代码
  1 BOOL
  2 IsAtArc(CAD_POINT firstPt,CAD_POINT secondPt,
  3 double radius,double direct,int More,CAD_POINT thePt)
  4 /**//* 判断某点是否在圆弧上
  5 firstPt 圆弧起点
  6 secondPt 圆弧终点 
  7 radius 半径
  8 direct 偏向( >=0__左偏 <0__右偏 )
  9 More (More<0__小圆弧,More>0__大圆弧)
 10 thePt 判断点
 11 返回:TRUE__在圆弧上 FALSE__不在圆弧上
 12 */
 13 {
 14 CAD_POINT centerPt,sectionPt;
 15 CAD_POINT startPt,endPt;
 16 double startAngle,endAngle,chordAngle,vertAngle;
 17 double intLine,chordLine;
 18 /**//* centerPt 圆弧圆心
 19 sectionPt 弦线中心点
 20 startAngle 圆弧起点切线角度(弧度)
 21 endAngle 圆弧终点切线角度(弧度)
 22 chordAngle 弦线角度(弧度)
 23 vertAngle 与弦线垂直的垂线角度(弧度)
 24 intLine 弦线中心至圆心距离
 25 chordLine 弦线长度
 26 */ 
 27 sectionPt.x = (firstPt.x + secondPt.x)/2;
 28 sectionPt.y = (firstPt.y + secondPt.y)/2;
 29 
 30 chordLine = sqrt( pow( (secondPt.x-firstPt.x),2 ) + pow( (secondPt.y-firstPt.y),2 ) );
 31 intLine = sqrt((radius*radius-chordLine*chordLine/4) );
 32 
 33 chordAngle = ads_angle(asDblArray(firstPt),asDblArray(secondPt)); //弦线的角度
 34 
 35 if(direct>=0)//左偏
 36 {
 37 startPt=firstPt;
 38 endPt=secondPt;
 39 vertAngle=chordAngle+Pai/2;
 40 }
 41 else if(direct<0)//右偏
 42 {
 43 startPt=secondPt;
 44 endPt=firstPt;
 45 vertAngle=chordAngle-Pai/2;
 46 }
 47 
 48 if(More<=0)//小圆弧
 49 {
 50 centerPt.x=sectionPt.x+intLine*cos(vertAngle);
 51 centerPt.y=sectionPt.y+intLine*sin(vertAngle);
 52 }
 53 else//大圆弧
 54 {
 55 centerPt.x=sectionPt.x-intLine*cos(vertAngle);
 56 centerPt.y=sectionPt.y-intLine*sin(vertAngle);
 57 }
 58 
 59 if(fabs(centerPt.distanceTo(thePt)-radius)>1.0E-8)
 60 return FALSE;
 61 startAngle = ads_angle(asDblArray(centerPt),asDblArray(startPt)); 
 62 endAngle = ads_angle(asDblArray(centerPt),asDblArray(endPt)); 
 63 
 64 AcDbArc *pArc=new AcDbArc(centerPt,radius,startAngle,endAngle);
 65 AcDbLine *pLine=new AcDbLine(centerPt,thePt);
 66 AcGePoint3dArray Points;
 67 pLine->intersectWith(pArc,AcDb::kOnBothOperands,Points);
 68 if(Points.isEmpty()) return FALSE;
 69 
 70 return TRUE;
 71 }
 72 
 73 相object加xdata
 74 void affixXdata(char *appName, char *xData, AcDbObject *pObj)
 75 {
 76     //向AcDbObject添加扩展数据Xdata
 77     struct resbuf *pRb, *pTemp;
 78 
 79     acdbRegApp(appName);
 80     pRb = acutNewRb(AcDb::kDxfRegAppName);
 81     pTemp = pRb;
 82     pTemp->resval.rstring = new char[strlen(appName)+1];
 83     strcpy(pTemp->resval.rstring, appName);
 84 
 85     pTemp->rbnext = acutNewRb(AcDb::kDxfXdAsciiString);
 86     pTemp = pTemp->rbnext;
 87     pTemp->resval.rstring = new char[strlen(xData)+1];
 88     strcpy(pTemp->resval.rstring, xData);
 89 
 90     pObj->setXData(pRb);
 91     acutRelRb(pRb);
 92 }
 93 
 94 //添加扩展数据
 95 //实体添加扩展数据(字符串)
 96 bool AddXData(CString appName, AcDbObjectId entId,CString data)
 97 {
 98 //open entity for read
 99 AcDbEntity*pEnt;
100 Acad::ErrorStatus es=acdbOpenAcDbEntity(pEnt,entId,AcDb::kForRead);
101 if(es!=Acad::eOk)
102 {
103 ads_printf("error in open entity\n");
104 return false;
105 }
106 //get XData buffer
107 struct resbuf*pRb,*pTemp;
108 pRb=pEnt->xData(appName);
109 if(pRb!=NULL)//have XData
110 {
111 //pTemp移到表尾
112 pTemp=pRb;
113 for(pTemp=pRb;pTemp->rbnext!=NULL;pTemp=pTemp->rbnext){;}
114 }
115 else//NOT have XData
116 {
117 //create new xData
118 ads_regapp(appName);
119 pRb=ads_newrb(AcDb::kDxfRegAppName);
120 pRb->resval.rstring=(char*)malloc(appName.GetLength()+1);
121 strcpy(pRb->resval.rstring,appName);
122 pTemp=pRb;
123 }
124 //fill xData string
125 pTemp->rbnext=ads_newrb(AcDb::kDxfXdAsciiString);
126 pTemp=pTemp->rbnext;
127 pTemp->resval.rstring=(char*)malloc(data.GetLength()+1);
128 strcpy(pTemp->resval.rstring,data);
129 //add xData
130 es=pEnt->upgradeOpen();
131 if(es!=Acad::eOk)
132 {
133 ads_printf("\nError occur in updateOpen.");
134 pEnt->close();
135 ads_relrb(pRb);
136 return false;
137 }
138 es=pEnt->setXData(pRb);
139 if(es!=Acad::eOk)
140 {
141 ads_printf("\nError occur in setXData.");
142 pEnt->close();
143 ads_relrb(pRb);
144 return false;
145 }
146 //
147 pEnt->close();
148 ads_relrb(pRb);
149 return true;
150 }
复制代码

 

复制代码
 1 //向object加xdata
 2     void affixXdata(char *appName, char *xData, AcDbObject *pObj)
 3 {
 4     //向AcDbObject添加扩展数据Xdata
 5     struct resbuf *pRb, *pTemp;
 6 
 7     acdbRegApp(appName);
 8     pRb = acutNewRb(AcDb::kDxfRegAppName);
 9     pTemp = pRb;
10     pTemp->resval.rstring = new char[strlen(appName)+1];
11     strcpy(pTemp->resval.rstring, appName);
12 
13     pTemp->rbnext = acutNewRb(AcDb::kDxfXdAsciiString);
14     pTemp = pTemp->rbnext;
15     pTemp->resval.rstring = new char[strlen(xData)+1];
16     strcpy(pTemp->resval.rstring, xData);
17 
18     pObj->setXData(pRb);
19     acutRelRb(pRb);
20 }
复制代码

 

复制代码
 1 //添加扩展数据
 2 //实体添加扩展数据(字符串)
 3 bool AddXData(CString appName, AcDbObjectId entId,CString data)
 4 {
 5     //open entity for read
 6     AcDbEntity*pEnt;
 7     Acad::ErrorStatus es=acdbOpenAcDbEntity(pEnt,entId,AcDb::kForRead);
 8     if(es!=Acad::eOk)
 9     {
10         ads_printf("error in open entity\n");
11         return false;
12     }
13     //get XData buffer
14     struct resbuf*pRb,*pTemp;
15     pRb=pEnt->xData(appName);
16     if(pRb!=NULL)//have XData
17     {
18         //pTemp移到表尾
19         pTemp=pRb;
20         for(pTemp=pRb;pTemp->rbnext!=NULL;pTemp=pTemp->rbnext){;}
21     }
22     else//NOT have XData
23     {
24         //create new xData
25         ads_regapp(appName);
26         pRb=ads_newrb(AcDb::kDxfRegAppName);
27         pRb->resval.rstring=(char*)malloc(appName.GetLength()+1);
28         strcpy(pRb->resval.rstring,appName);
29         pTemp=pRb;
30     }
31     //fill xData string
32     pTemp->rbnext=ads_newrb(AcDb::kDxfXdAsciiString);
33     pTemp=pTemp->rbnext;
34     pTemp->resval.rstring=(char*)malloc(data.GetLength()+1);
35     strcpy(pTemp->resval.rstring,data);
36     //add xData
37     es=pEnt->upgradeOpen();
38     if(es!=Acad::eOk)
39     {
40         ads_printf("\nError occur in updateOpen.");
41         pEnt->close();
42         ads_relrb(pRb);
43         return false;
44     }
45     es=pEnt->setXData(pRb);
46     if(es!=Acad::eOk)
47     {
48         ads_printf("\nError occur in setXData.");
49         pEnt->close();
50         ads_relrb(pRb);
51         return false;
52     }
53     //
54     pEnt->close();
55     ads_relrb(pRb);
56     return true;
57 }
复制代码

你可能感兴趣的:(ObjectARX代码片段二)