• objectARX常用代码2


    objectARX常用代码2

    设置当前的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;
    }


    //功能描述:选择指定图层上的所有实体!

     // ************************************************************************
     // 函数名称: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;
    }
    另一种实现
    //************************************************************************
    //函数名称: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;
    }



    生成新组
    //生成新组(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;
    }

    另一个
    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(" 输入的词典名无效!");
    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 pGroup->append(IdArray[i]);
    pGroup->setSelectable(FALSE);
    pGroupDict->close();
    pGroup->close();
    return es;
    }

    旋转整个group
    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();
    }


    新建一个图层
    //==========================================================
    功能:新建一个图层
    参数: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;

    }; 


    create 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;

    }


    前阵困扰我的问题解决了,现在共享给大家:
    //************************************************************************
    //函数名称: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
    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;
    }

    相object加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 ");
    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(" Error occur in updateOpen.");
    pEnt->close();
    ads_relrb(pRb);
    return false;
    }
    es=pEnt->setXData(pRb);
    if(es!=Acad::eOk)
    {
    ads_printf(" Error occur in setXData.");
    pEnt->close();
    ads_relrb(pRb);
    return false;
    }
    //
    pEnt->close();
    ads_relrb(pRb);
    return true;
    }
  • 相关阅读:
    SpannaleString总结
    【未完成】bug记录2013427>import工程时出现Build path contains duplicate entry:'src' for project 'XXX'
    【未完成】给eclipse项目改名
    android创建和删除桌面快捷方式
    bug记录2013426(2)>Select at least one project错误
    hosts配置
    获取当前应用的版本号及android系统版本号及手机型号
    转载:如何将offcie 2003文档(.doc、.xls、.ppt)转换成mht文档
    转载:.NET2.0 验证控件常用的正则表达式
    转载: RESTORE DATABASE命令还原SQLServer 2005 数据库
  • 原文地址:https://www.cnblogs.com/mjgw/p/12406100.html
Copyright © 2020-2023  润新知