sophie_wzh 发表于 2004-5-20 08:01:00

在ARX中如何定义一个复杂块,望高手指点

在ARX中如何定义一个复杂块,然后插入模型空间中?.望高手指点, qq 172675612

tchg 发表于 2004-5-21 20:36:00

Acad::ErrorStatus createBlockRecord (const char *name) {<BR>        // First, check if a block of the same name already exists<BR>        // by verifying in the current database block table.<BR>        AcDbBlockTable *pBlockTable ;<BR>        // Open the block table for read<BR>        Acad::ErrorStatus es ;<BR>        if ( (es =acdbHostApplicationServices ()-&gt;workingDatabase ()-&gt;getBlockTable (pBlockTable, AcDb::kForRead)) != Acad::eOk )<BR>                return (es) ;


        if ( pBlockTable-&gt;has (name) == Adesk::kTrue ) {<BR>                pBlockTable-&gt;close () ;<BR>                return (Acad::eDuplicateKey) ;<BR>        }<BR>        // Now we know the block does not exist, so we create it<BR>        // using the name passed in.<BR>        AcDbBlockTableRecord *pBlockTableRecord =new AcDbBlockTableRecord ;<BR>        pBlockTableRecord-&gt;setName (name) ;<BR>        // To keep it simple, we use the origin for the insertion point<BR>        pBlockTableRecord-&gt;setOrigin (AcGePoint3d::kOrigin) ;<BR>        // Open the block table for write<BR>        // since we are adding a new block definition<BR>        if ( (es =pBlockTable-&gt;upgradeOpen ()) != Acad::eOk ) {<BR>                delete pBlockTableRecord ;<BR>                pBlockTable-&gt;close () ;<BR>                return (es) ;<BR>        }<BR>        // Add the new block table record to the block table.<BR>        // For now, the block table record is empty.<BR>        if ( (es =pBlockTable-&gt;add (pBlockTableRecord)) != Acad::eOk ) {<BR>                // The block table record has not been added<BR>                // to the block table, so we have to delete it.<BR>                pBlockTable-&gt;close();<BR>                delete pBlockTableRecord;<BR>                return (es) ;<BR>        }<BR>        pBlockTable-&gt;close () ;<BR>        // Now the block table record is in the database, but is empty<BR>        // (has no sub-entity).<BR>        // Note that after having been added to the database, an object or an entity<BR>        // is implicitely opened for write.<BR>        //<BR>        // So we create the sub entities to append to the block<BR>        // which will represent a "happy face":<BR>        // the block should consist of a round yellow face (circle)<BR>        // two blue eyes (circles) and a red mouth (arc)<BR>        AcDbCircle *pFace =new AcDbCircle (AcGePoint3d::kOrigin, AcGeVector3d::kZAxis, 1.0) ;<BR>        AcDbCircle *pLeftEye =new AcDbCircle (AcGePoint3d (0.33, 0.25, 0.0), AcGeVector3d::kZAxis, 0.1) ;<BR>        AcDbCircle *pRightEye =new AcDbCircle (AcGePoint3d (-0.33, 0.25, 0.0), AcGeVector3d::kZAxis, 0.1) ;<BR>        AcDbArc *pMouth =new AcDbArc (AcGePoint3d (0, 0.5, 0), 1.0, 3.141592 + (3.141592 * 0.3), 3.141592 + (3.141592 * 0.7)) ;<BR>        // Set the color property.<BR>        pFace-&gt;setColorIndex (2) ;<BR>        pLeftEye-&gt;setColorIndex (5) ;<BR>        pRightEye-&gt;setColorIndex (5) ;<BR>        pMouth-&gt;setColorIndex (1) ;<BR>        // add the entities to the new block table record<BR>        if ( (es =pBlockTableRecord-&gt;appendAcDbEntity (pFace)) != Acad::eOk ) {<BR>                delete pFace ;<BR>                delete pLeftEye ;<BR>                delete pRightEye ;<BR>                delete pMouth ;<BR>                pBlockTableRecord-&gt;erase () ;<BR>                pBlockTableRecord-&gt;close () ;<BR>                return (es) ;<BR>        }<BR>        pFace-&gt;close () ;


        if ( (es =pBlockTableRecord-&gt;appendAcDbEntity (pLeftEye)) != Acad::eOk ) {<BR>                delete pLeftEye ;<BR>                delete pRightEye ;<BR>                delete pMouth ;<BR>                pBlockTableRecord-&gt;erase () ;<BR>                pBlockTableRecord-&gt;close () ;<BR>                return (es) ;<BR>        }<BR>        pLeftEye-&gt;close () ;


        if ( (es =pBlockTableRecord-&gt;appendAcDbEntity (pRightEye)) != Acad::eOk ) {<BR>                delete pRightEye ;<BR>                delete pMouth ;<BR>                pBlockTableRecord-&gt;erase () ;<BR>                pBlockTableRecord-&gt;close () ;<BR>                return (es) ;<BR>        }<BR>        pRightEye-&gt;close () ;


        if ( (es =pBlockTableRecord-&gt;appendAcDbEntity (pMouth)) != Acad::eOk ) {<BR>                delete pMouth ;<BR>                pBlockTableRecord-&gt;erase () ;<BR>                pBlockTableRecord-&gt;close () ;<BR>                return (es) ;<BR>        }<BR>        pMouth-&gt;close () ;


        pBlockTableRecord-&gt;close () ;


        return (Acad::eOk) ;<BR>}


插入块 输入块的名称


void insrtBlk()<BR>{<BR>        char blkName;<BR>        AcDbDatabase *pCurDb;<BR>        AcDbBlockTable *pBlkTable;<BR>        AcDbBlockTableRecord *pBlkTableRecord;<BR>        AcDbBlockReference *pInsrtObj;<BR>        AcDbObjectId blkId;


        AcGePoint3d insPt;


        int retCode;


        retCode = acedGetString(0, "\nEnter Block Name: ", blkName);<BR>        if(retCode != RTNORM || blkName == '\0')<BR>        {<BR>                acutPrintf("\nInvalid block name.");<BR>                return;<BR>        }


        pCurDb = acdbHostApplicationServices()-&gt;workingDatabase();


        // Check to see if the block table<BR>        // has blkName


        pCurDb-&gt;getBlockTable(pBlkTable, AcDb::kForRead);<BR>        if(!pBlkTable-&gt;has(blkName))<BR>        {<BR>                acutPrintf("\nBlock definition %s not found. ", blkName);<BR>                pBlkTable-&gt;close();<BR>                return;<BR>        }


        // Get the AcDbObjectId of the block<BR>        // definition.<BR>        pBlkTable-&gt;getAt(blkName, blkId);


        pBlkTable-&gt;getAt(ACDB_MODEL_SPACE, pBlkTableRecord, AcDb::kForWrite);<BR>        pBlkTable-&gt;close();<BR>        <BR>        acedInitGet(RSG_NONULL, NULL);<BR>        acedGetPoint(NULL, "\nPick insertion point: ", asDblArray(insPt));


        pInsrtObj = new AcDbBlockReference(insPt, blkId);


        // Here is where you can set scale, rotation and other<BR>        // properties to the block entity. If you want to<BR>        // see the AcDbBlockReference class for more details.


        pBlkTableRecord-&gt;appendAcDbEntity(blkId, pInsrtObj);


        pBlkTableRecord-&gt;close();<BR>        pInsrtObj-&gt;close();<BR>}

Pangge_118 发表于 2004-5-24 15:47:00

// This is command 'INSERTBLK'<BR>void defineblockwithattrib()<BR>{<BR>                       AcDbObjectId blockId;<BR>        AcGePoint3d basePoint(0.0,0.0,0.0);<BR>        double textHeight=3.5;<BR>        double textAngle=0.0;


        int retCode = 0;<BR>        AcDbDatabase *pCurDb;<BR>        AcDbBlockTable *pBlockTable = NULL;<BR>        AcDbBlockTableRecord *pBlockRecord = new AcDbBlockTableRecord;<BR>        AcDbObjectId entityId;<BR>        AcGeMatrix3d ucsMat;


        //第一步:设置块定义的块名和基点<BR>        pBlockRecord-&gt;setName("ROUGHNESS");<BR>        pBlockRecord-&gt;setOrigin(basePoint);<BR>        //写状态下打开块表<BR>        pCurDb = acdbHostApplicationServices()-&gt;workingDatabase();<BR>        pCurDb-&gt;getBlockTable(pBlockTable,AcDb::kForWrite);


        //第二步:增加块表记录到块表<BR>                       pBlockTable-&gt;add(blockId,pBlockRecord);


        //第三步:创建粗糙度标注符号实体<BR>        AcGePoint2d plinePt;<BR>        AcGePoint2dArray plinePtArr;


        plinePt.x=1.4*tan(PI/6.0)*textHeight;<BR>        plinePt.y=1.4*textHeight;<BR>        plinePtArr.append(plinePt);


        plinePt.x=-1.4*tan(PI/6.0)*textHeight;<BR>        plinePt.y=1.4*textHeight;<BR>        plinePtArr.append(plinePt);


        plinePt.x=0.0;<BR>        plinePt.y=0.0;<BR>        plinePtArr.append(plinePt);


        plinePt.x=2.8*tan(PI/6)*textHeight;<BR>        plinePt.y=2.8*textHeight;<BR>        plinePtArr.append(plinePt);


        AcDbPolyline *pLine = new AcDbPolyline(4);<BR>        for(int idx=0;idx&lt;4;idx++)<BR>        {<BR>                       plinePt=plinePtArr.at(idx);<BR>                       pLine-&gt;addVertexAt(idx,plinePt);<BR>        }


        //把粗糙度标注符号实体加入块表记录中


        pBlockRecord-&gt;appendAcDbEntity(entityId,pLine);<BR>        pLine-&gt;close();


<BR>        //第四步:创建属性定义<BR>        AcDbAttributeDefinition *pAttdef<BR>        =new AcDbAttributeDefinition;


        //设置属性定义值


                       //定义属性的位置<BR>        AcGePoint3d attposition(-5.0,6.0,0.0);<BR>        pAttdef-&gt;setPosition(attposition);<BR>        pAttdef-&gt;setHeight(textHeight);<BR>        pAttdef-&gt;setRotation(textAngle);<BR>        pAttdef-&gt;setHorizontalMode(AcDb::kTextLeft);<BR>        pAttdef-&gt;setVerticalMode(AcDb::kTextBase);<BR>        pAttdef-&gt;setPrompt("粗糙度");<BR>        pAttdef-&gt;setTextString("3.2");<BR>        pAttdef-&gt;setTag("粗糙度");<BR>        pAttdef-&gt;setInvisible(Adesk::kFalse);<BR>        pAttdef-&gt;setVerifiable(Adesk::kFalse);<BR>        pAttdef-&gt;setPreset(Adesk::kFalse);<BR>        pAttdef-&gt;setConstant(Adesk::kFalse);<BR>        pAttdef-&gt;setFieldLength(4);


        //把属性定义加入块中


        pBlockRecord-&gt;appendAcDbEntity(entityId,pAttdef);<BR>        pAttdef-&gt;close();<BR>        pBlockRecord-&gt;close();<BR>        pBlockTable-&gt;close();<BR>        return;        <BR>}


// This is command 'INSBLOCK'<BR>void insblock()<BR>{<BR>        // TODO: Implement the command<BR>        char blkName;<BR>        AcDbDatabase *pCurDb;<BR>        AcDbBlockTable *pBlkTable;<BR>        AcDbBlockTableRecord *pBlkTableRecord;<BR>        AcDbBlockTableRecord *pBlkDefRecord;<BR>        AcDbBlockReference *pInsrtObj;<BR>        AcDbEntity *pEnt;<BR>        AcDbBlockTableRecordIterator *pIterator;<BR>        AcDbAttributeDefinition *pAttDef;<BR>        AcDbAttribute *pAtt;<BR>        AcDbObjectId blkId;<BR>        AcDbObjectId insrtId;<BR>                       char *pTagPrompt;<BR>        AcGePoint3d insPt;<BR>        AcGePoint3d basePt;<BR>        int retCode;<BR>//第一步:判断块是不是存在<BR>        retCode=acedGetString(0,"\nEnter Block Name:",blkName);<BR>        if(retCode != RTNORM || blkName=='\0')<BR>        {<BR>               acutPrintf("\n Invalid block name");<BR>               return;<BR>        }


        pCurDb = acdbHostApplicationServices()-&gt;workingDatabase();<BR>        pCurDb-&gt;getBlockTable(pBlkTable,AcDb::kForRead);<BR>        if(!pBlkTable-&gt;has(blkName))<BR>        {<BR>                       acutPrintf("\nBlock definition %s not found.",blkName);<BR>                       pBlkTable-&gt;close();<BR>                       return;<BR>        }


        //Get the AcDbObjectId of the block definition.<BR>        pBlkTable-&gt;getAt(blkName,blkId);<BR>        //Get the block record of the model space to add the <BR>        //block reference<BR>        pBlkTable-&gt;getAt(ACDB_MODEL_SPACE,pBlkTableRecord,AcDb::kForWrite);<BR>        pBlkTable-&gt;close();


//第二步:创建块参考实体<BR>        acedInitGet(RSG_NONULL,NULL);<BR>        acedGetPoint(NULL,"\nPick insertion point:",asDblArray(insPt));<BR>        pInsrtObj = new AcDbBlockReference(insPt,blkId);


        //here is where you can set scale.rotation and other<BR>        //properties to the block entity. if you want to <BR>        //see the AcDbBlockReference class for more details


        //               pBlkTableRecord-&gt;appendAcDbEntity(insrtId,pInsrtObj);


//第三步:根据块中的属性定义来定义一个属性实体,并把属性实体加入块参考对象中<BR>        acdbOpenObject(pBlkDefRecord,blkId,AcDb::kForWrite);<BR>                       //Now check to see if the block Definition<BR>        //has attributes.if if does we will add <BR>        //a Block Table Record Iterator to step through<BR>        //the entity and find the attribute Definitions.


        if(pBlkDefRecord-&gt;hasAttributeDefinitions())<BR>        {<BR>                               pBlkDefRecord-&gt;newIterator(pIterator);<BR>                //check to see if the entity is a attribue definition.<BR>                for(pIterator-&gt;start();!pIterator-&gt;done();<BR>                pIterator-&gt;step())<BR>                {<BR>                        pIterator-&gt;getEntity(pEnt,AcDb::kForRead);<BR>                        //check to see if the entity is an attribute definition<BR>                        pAttDef = AcDbAttributeDefinition::cast(pEnt);<BR>                        if(pAttDef != NULL &amp;&amp; !pAttDef-&gt;isConstant())<BR>                        {<BR>                               //if it is and it's not constant<BR>                               //create a new attribute<BR>                                       pAtt = new AcDbAttribute();


                                       //setPropertiesFrom will copy Color.<BR>                                       //Layer,Linetype,Linetype Scale and Visiblity<BR>                                       pAtt-&gt;setPropertiesFrom(pAttDef);<BR>                                       //setup more properties from the attribute definition<BR>                                       pAtt-&gt;setInvisible(pAttDef-&gt;isInvisible());<BR>                                       basePt = pAttDef-&gt;position();<BR>                                       basePt += pInsrtObj-&gt;position().asVector();<BR>                                                                                                               pAtt-&gt;setPosition(basePt);<BR>                                       pAtt-&gt;setHeight(pAttDef-&gt;height());<BR>                                       pAtt-&gt;setRotation(pAttDef-&gt;rotation());


                                       //take note how we get the tag.<BR>                                       pTagPrompt = pAttDef-&gt;tag();<BR>                                       pAtt-&gt;setTag(pTagPrompt);<BR>                                       free(pTagPrompt);<BR>                                       //normally you would prompt the user<BR>                                       //and ask for input values<BR>                                       pTagPrompt = pAttDef-&gt;prompt();<BR>                                       acutPrintf("%s%s","\n",pTagPrompt);<BR>                                       free(pTagPrompt);


                                       //The setFieldLength is not required<BR>                                       //even though it is listed in the documentation<BR>                                       pAtt-&gt;setFieldLength(25);


                                       //setTextString is the value the <BR>                                       //attribute recieves which would<BR>                                       //normally be a user input value.<BR>                                       pAtt-&gt;setTextString("6.4");<BR>                                       //将属性加入块参考对象中去<BR>                                       pInsrtObj-&gt;appendAttribute(pAtt);<BR>                                       pAtt-&gt;close();<BR>                        }//end if<BR>                                                                                       pEnt-&gt;close();<BR>                }//end for<BR>        } //end if<BR>                       pBlkTableRecord-&gt;appendAcDbEntity(insrtId,pInsrtObj);<BR>        delete pIterator;<BR>        //Note that we close the Model space block<BR>        //table record after we have added our attribute<BR>        pBlkTableRecord-&gt;close();<BR>        pBlkDefRecord-&gt;close();<BR>        pInsrtObj-&gt;close();


}
页: [1]
查看完整版本: 在ARX中如何定义一个复杂块,望高手指点