ObjectArx的一次常用方法

1.画一条直线

    static void ArxProject1_DrawLine(void)
    {

        
        //定义两点确定一条直线
        AcGePoint3d ptStart( 0 , 0 , 0);
        AcGePoint3d ptEnd( 100 , 100 , 0);
        AcDbLine* lnNew = new AcDbLine( ptStart , ptEnd);
        
     //获取当前图形数据库 AcDbBlockTable
* pBlockTable = NULL; acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlockTable , AcDb::kForWrite); AcDbBlockTableRecord* pBlockTableRecord = NULL; pBlockTable->getAt(ACDB_MODEL_SPACE , pBlockTableRecord , AcDb::kForWrite);      
     //插入当前的图形数据库 AcDbObjectId lnId; pBlockTableRecord
->appendAcDbEntity( lnId , lnNew);     
     //关闭对象 lnNew
->close(); pBlockTableRecord->close(); pBlockTable->close(); }

2.画一段圆弧

    static void ArxProject1_DrawArc(void)
    {
        //三点创建一个圆弧
        AcGePoint2d ptStartPoint( 0 , 100 );
        AcGePoint2d ptEndPoint( 0 , 100 );
        AcGePoint2d ptCenterPoint(0 , 0);

        AcGeVector2d vecStart(ptStartPoint.x - ptCenterPoint.x ,  ptStartPoint.y - ptCenterPoint.y);
        AcGeVector2d vecEnd(ptEndPoint.x - ptCenterPoint.x ,  ptEndPoint.y - ptCenterPoint.y);
        double StartAng = vecStart.angle();
        double EndAng = vecEnd.angle();
        
        AcGePoint3d pt( 0 , 0, 0);
        AcGeVector3d vec( 0 , 0 ,1);
        AcDbArc* Acr = NULL;
        Acr = new AcDbArc(pt ,vec ,  100, StartAng , EndAng);

        AcDbBlockTable* pBlockTable = NULL;
        ErrorStatus es =  acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlockTable , AcDb::kForWrite);

        AcDbBlockTableRecord* pBlockTableRecord = NULL;
        es = pBlockTable->getAt(ACDB_MODEL_SPACE , pBlockTableRecord,  AcDb::kForWrite);
        
        AcDbObjectId oid;
        es = pBlockTableRecord->appendAcDbEntity(oid , Acr);

        Acr->close();
        pBlockTableRecord->close();
        pBlockTable->close();

    }

3.创建一个块,并把它插入到当前的模型空间

    static void ArxProject1_CreateNewBlock(void)
    {
        // Add your code for command ArxProject1._CreateNewBlock here
        ErrorStatus es ;
        
        AcDbBlockTable* pBlockTable = NULL;
        acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlockTable , AcDb::kForWrite);

        AcDbBlockTableRecord* pBlockTableRecord = new AcDbBlockTableRecord();
        pBlockTableRecord->setName(_T("Hest"));
        
        AcDbObjectId BlockTableOid;
        pBlockTable->add(BlockTableOid , pBlockTableRecord);

        AcDbPolyline* pPloyLine= new AcDbPolyline();
        AcGePoint2d pt1(0 , 0);
        AcGePoint2d pt2(100,200);
        pPloyLine->addVertexAt( 0 , pt1);;
        pPloyLine->addVertexAt( 1 , pt2);
        AcDbObjectId oid;
        pBlockTableRecord->appendAcDbEntity(oid , pPloyLine);

        pPloyLine->close();
        pBlockTableRecord->close();
        pBlockTable->close();
    }

    // - ArxProject1._BlockInsert command (do not rename)
    static void ArxProject1_BlockInsert(void)
    {
        // Add your code for command ArxProject1._BlockInsert here
        AcDbBlockTable* pBlockTable = NULL;
        acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlockTable , AcDb::kForRead);
        if (!pBlockTable->has(_T("Hest")))
        {
            acutPrintf(_T("不存在块Hest"));
            pBlockTable->close();
            return;
        }

        ads_point pt;
        if (acedGetPoint( NULL , _T("\n请输入插入点") , pt) != RTNORM)
        {
            pBlockTable->close();
            return;
        }
        
        AcGePosition3d pt3d = asPnt3d(pt); 
        AcDbObjectId oid;
        pBlockTable->getAt(_T("Hest") , oid );
        AcDbBlockReference* pBlockRef = new AcDbBlockReference( pt3d , oid);

        AcDbBlockTableRecord* pBlockTableRecord = NULL;
        pBlockTable->getAt(ACDB_MODEL_SPACE , pBlockTableRecord , AcDb::kForWrite);
        AcDbObjectId refid;
        pBlockTableRecord->appendAcDbEntity(refid , pBlockRef);

        pBlockRef->close();
        pBlockTableRecord->close();
        pBlockTable->close();
    }

4.创建一个新的图层

    static void ArxProject1_CreateNewBlock(void)
    {
        // Add your code for command ArxProject1._CreateNewBlock here
        ErrorStatus es ;
        
        AcDbBlockTable* pBlockTable = NULL;
        acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlockTable , AcDb::kForWrite);

        AcDbBlockTableRecord* pBlockTableRecord = new AcDbBlockTableRecord();
        pBlockTableRecord->setName(_T("Hest"));
        
        AcDbObjectId BlockTableOid;
        pBlockTable->add(BlockTableOid , pBlockTableRecord);

        AcDbPolyline* pPloyLine= new AcDbPolyline();
        AcGePoint2d pt1(0 , 0);
        AcGePoint2d pt2(100,200);
        pPloyLine->addVertexAt( 0 , pt1);;
        pPloyLine->addVertexAt( 1 , pt2);
        AcDbObjectId oid;
        pBlockTableRecord->appendAcDbEntity(oid , pPloyLine);

        pPloyLine->close();
        pBlockTableRecord->close();
        pBlockTable->close();
    }

    // - ArxProject1._BlockInsert command (do not rename)
    static void ArxProject1_BlockInsert(void)
    {
        // Add your code for command ArxProject1._BlockInsert here
        AcDbBlockTable* pBlockTable = NULL;
        acdbHostApplicationServices()->workingDatabase()->getBlockTable(pBlockTable , AcDb::kForRead);
        if (!pBlockTable->has(_T("Hest")))
        {
            acutPrintf(_T("不存在块Hest"));
            pBlockTable->close();
            return;
        }

        ads_point pt;
        if (acedGetPoint( NULL , _T("\n请输入插入点") , pt) != RTNORM)
        {
            pBlockTable->close();
            return;
        }
        
        AcGePosition3d pt3d = asPnt3d(pt); 
        AcDbObjectId oid;
        pBlockTable->getAt(_T("Hest") , oid );
        AcDbBlockReference* pBlockRef = new AcDbBlockReference( pt3d , oid);

        AcDbBlockTableRecord* pBlockTableRecord = NULL;
        pBlockTable->getAt(ACDB_MODEL_SPACE , pBlockTableRecord , AcDb::kForWrite);
        AcDbObjectId refid;
        pBlockTableRecord->appendAcDbEntity(refid , pBlockRef);

        pBlockRef->close();
        pBlockTableRecord->close();
        pBlockTable->close();
    }

5.插入一个外部的dwg文件,以块的方式

    static void ArxProject1_InsertDwg(void)
    {
        // Add your code for command ArxProject1._InsertDwgB here
        AcDbDatabase* pNewDb = new AcDbDatabase(Adesk::kFalse);
        AcApDocument* pcurDoc = NULL;
        pcurDoc = curDoc();
        if (pcurDoc == NULL)
        {
            assert(FALSE);
            return;
        }
        acDocManager->lockDocument(pcurDoc);
        Acad::ErrorStatus es;
        es = pNewDb->readDwgFile(_T("D://测试数据//528500-2524250-250-250.dwg")  , _SH_DENYNO , FALSE);
        if (es != ErrorStatus::eOk) 
        {
            acutPrintf(_T("Read Dwg fail"));
            return ;
        }
        AcDbDatabase* pCurDb = acdbHostApplicationServices()->workingDatabase();
        AcDbObjectId oid;
        es  = pCurDb->insert(oid , _T("InsertDwg") , pNewDb ,true);
        if (es != ErrorStatus::eOk)
        {
            assert(FALSE);
            return;
        }
        
        AcDbBlockReference* pRef = new AcDbBlockReference();
        pRef->setBlockTableRecord(oid);
        AcDbBlockTable* pBolck = NULL;
        pCurDb->getBlockTable(pBolck , AcDb::kForWrite);
        AcDbBlockTableRecord* pBolckRecord = NULL;
        pBolck->getAt(ACDB_MODEL_SPACE , pBolckRecord , AcDb::kForWrite);
        AcDbObjectId refid;
        pBolckRecord->appendAcDbEntity(refid , pRef);
        pRef->close();
        pRef = NULL;
        pBolck->close();
        pBolck = NULL;
        pBolckRecord->close();
        pBolckRecord = NULL;
        acDocManager->unlockDocument(pcurDoc);
    
    }

6.插入一张图片作为外部参照

    static void ArxProject1_InsertImg(void)
    {
            CString strImgPath = _T("C:\\opencv\\samples\\cpp\\tutorial_code\\images\\yellowball.jpg");
            AcDbObjectId oid;
            //insertIMGFile( oid , _T("0") , strImgPath , _T("yellowball") , AcGePoint2d( 0 , 0 ) , AcGePoint2d( 100 , 100));
            InsPic(strImgPath);
            return ;

    }
        
    static bool InsPic(CString strPath)
    {    
        #if _MSC_VER >= 1600
        if (!acrxDynamicLinker->loadModule(_T("acISMobj19.dbx"), true))
        {
            acutPrintf(_T("无法加载acISMobj19.dbx\n"));
        }
        #elif _MSC_VER >= 1500
        if (!acrxDynamicLinker->loadModule(_T("acISMobj18.dbx"), true))
        {
            acutPrintf(_T("无法加载acISMobj18.dbx\n"));
        }
        #elif _MSC_VER >= 1400
        //一定要加载此文件,否则加载影像图时会失败
        if (!acrxDynamicLinker->loadModule(_T("acISMobj17.dbx"), true))
        {
            acutPrintf(_T("无法加载acISMobj17.dbx\n"));
        }
        #elif _MSC_VER >= 1300
        if (!acrxDynamicLinker->loadModule(_T("acISMobj16.dbx"), true))
        {
            acutPrintf(_T("无法加载acISMobj16.dbx\n"));
        }
        #else
        if (!acrxDynamicLinker->loadModule(_T("acISMobj15.dbx"), true))
        {
            acutPrintf(_T("无法加载acISMobj15.dbx\n"));
        }
        #endif
        AcDbObjectId imgDicId =
                AcDbRasterImageDef::imageDictionary(acdbHostApplicationServices()
                ->workingDatabase());

        Acad::ErrorStatus es;
        if (imgDicId.isNull())
        {
                es = AcDbRasterImageDef::createImageDictionary(acdbHostApplicationServices()
                        ->workingDatabase(), imgDicId);
        }

        AcDbDictionary *pDic = NULL;
        es = acdbOpenObject((AcDbObject*&)pDic, imgDicId, AcDb::kForWrite);

        AcDbRasterImageDef *pImgDef = new AcDbRasterImageDef;

        es = pImgDef->setSourceFileName(strPath);
        es = pImgDef->load();

        CString strImg;
        es = AcDbRasterImageDef::suggestName(pDic, strPath, strImg.GetBuffer(256));

        AcDbObjectId entId;

        bool bHas = pDic->has(strImg);

        if (!bHas)
        {
                es = pDic->setAt(strImg, pImgDef, entId);
        }

        else
        {
                pDic->close();
                delete pImgDef;
                return false;
        }
        pDic->close();
        pImgDef->close();

        AcDbRasterImage *pImg = new AcDbRasterImage;

         AcGeVector3d uVec,vVec; 
         uVec.set(50, 0,0);          
         uVec.set(0, 150, 0);          
         AcGePoint3d pt(100, 100, 0);  
        BOOL bSet = pImg->setOrientation(pt, uVec, vVec);
        pImg->setFade(5);
        pImg->setContrast(9);
        

        es = pImg->setImageDefId(entId);
        
        AcDbBlockTable* pBlockTable = NULL;
        es = acdbHostApplicationServices()->workingDatabase()->getBlockTable( pBlockTable , AcDb::kForWrite);
        if(es != ErrorStatus::eOk)
            return FALSE;

        AcDbBlockTableRecord* pBlockTableRecord = NULL;
        es = pBlockTable->getAt(ACDB_MODEL_SPACE , pBlockTableRecord , AcDb::kForWrite);
        if(es != ErrorStatus::eOk)
            return false;

        AcDbObjectId oid;
        es = pBlockTableRecord->appendAcDbEntity(oid , pImg);
        if(es != ErrorStatus::eOk)
            return false;
        AcDbObjectId entIdx  = pImg->objectId();

        pImg->close();
        return true;
}

7.插入dwg作为外部参照

    static void ArxProject1_Addxref(void)
    {
        // Add your code for command ArxProject1._Addxref here
        AcDbObjectId oid;
        ACHAR lpszBlockName[255];
        ACHAR* lpszFileName = _T("C:\\XREGER.dwg");
        ErrorStatus es = acedXrefAttach( lpszFileName , lpszBlockName);
        if (es != ErrorStatus::eOk)
        {
            acutPrintf(_T("添加Xref失败!\n"));
        }
    }

 

posted @ 2014-10-22 16:59  三井瘦  阅读(3147)  评论(0编辑  收藏  举报