天气与日历 切换到窄版

 找回密码
 立即注册
中国膜结构网
十大进口膜材评选 十大国产膜材评选 十大膜结构设计评选 十大膜结构公司评选
查看: 62|回复: 0

arx创建块源码

[复制链接]

该用户从未签到

主题

0

回帖

2912

积分

管理员

积分
2912
发表于 2024-6-22 09:46:18 | 显示全部楼层 |阅读模式
[code]class BlockDrawCar
{
public:
        void DrawBlock(AcDbBlockTableRecord *btr)
        {
                Acad::ErrorStatus es;

                // Draw a rectangle polyline
                AcDbPolyline *pline = new AcDbPolyline();
                AcGePoint2d p0(0.0, 0.0), p1(m_Length, 0.0);
                AcGePoint2d p2(m_Length, m_Width), p3(0.0, m_Width);
                pline->addVertexAt(0, p0);
                pline->addVertexAt(1, p1);
                pline->addVertexAt(2, p2);
                pline->addVertexAt(3, p3);
                pline->setClosed(true);
                pline->setDatabaseDefaults();
                pline->setColorIndex(m_Color);
                es = btr->appendAcDbEntity(pline);
                if (!es)
                        pline->close();
                else
                        delete pline;

                // Draw a circle
                AcDbCircle *circle = new AcDbCircle();
                AcGePoint3d center(m_Width / 2, m_Width / 2, 0.0);
                double radius = m_Width * 0.4;
                circle->setCenter(center);
                circle->setRadius(radius);
                circle->setDatabaseDefaults();
                circle->setColorIndex(m_Color);
                es = btr->appendAcDbEntity(circle);
                if (!es)
                        circle->close();
                else
                        delete circle;
        }

public:
        int m_Color=7;
        double m_Width=200;
        double m_Length=340;
};


AcDbObjectId CreateCarBlock(AcDbDatabase *db, LPCWSTR blockname)
{
        Acad::ErrorStatus es;
        AcDbBlockTableRecord *btr = new AcDbBlockTableRecord;
        BlockDrawCar *car = new BlockDrawCar;
        car->DrawBlock(btr);
        btr->setName(blockname);

        // Add the BTR to the Blocktable of the current drawing
        AcDbObjectId BtrID;
        AcDbBlockTable *blocktable;
        es = db->getBlockTable(blocktable, AcDb::kForWrite);
        if (!es)
        {
                es = blocktable->add(BtrID, btr);
                if (!es)
                        btr->close();
                else
                        delete btr;
                blocktable->close();
        }

        return BtrID;
}

AcDbObjectId FindBlock(AcDbDatabase *db, LPCWSTR blockname)
{
        AcDbObjectId BlockID;
        Acad::ErrorStatus es;
        AcDbBlockTable *blocktable;
        es = db->getBlockTable(blocktable, AcDb::kForRead);
        if (!es)
        {
                es = blocktable->getAt(blockname, BlockID);
                blocktable->close();
        }
        return BlockID;
}

Acad::ErrorStatus PostToDb(AcDbDatabase *db, AcDbObjectId& objId, AcDbEntity* pEnt)
{
        Acad::ErrorStatus      es;
        AcDbBlockTable*        blocktable = 0;
        AcDbBlockTableRecord*  modelspace = 0;

        if ((es = db->getBlockTable(blocktable, AcDb::kForRead)) != Acad::eOk)
                return es;

        if ((es = blocktable->getAt(ACDB_MODEL_SPACE, modelspace, AcDb::kForWrite)) == Acad::eOk)
        {
                es = modelspace->appendAcDbEntity(objId, pEnt);
                modelspace->close();
        }
        blocktable->close();

        return es;
}

void cmdDrawCar()
{
        Acad::ErrorStatus es;
        LPCWSTR blockname = L"*U"; // Blockname. This is for an unnamed block.
        blockname = L"car"; // And this for a named block

        AcDbDatabase *db = acdbHostApplicationServices()->workingDatabase();
        AcDbObjectId BtrID;
        if (blockname[0]!=L'*') // '*' as first char means: Unnamed block. Don't search for it.
                BtrID = FindBlock(db, blockname); // Search for an existing named block

        if (BtrID.isNull())
                BtrID = CreateCarBlock(db, blockname); // For "*U" AutoCAD will create unique names like "*U1", "*U2", ...

        if (BtrID.isNull())
        {
                acutPrintf(L"\nFailed.");
                return;
        }

        // Now create a block reference
        AcDbBlockReference *bref = new AcDbBlockReference;
        bref->setDatabaseDefaults();
        bref->setBlockTableRecord(BtrID);

        AcGeVector3d insertionPoint;
        acedGetPoint(nullptr, L"\nInsertion point: ", asDblArray(insertionPoint));
        AcGeMatrix3d trans; // the transformation matrix
        trans.setToTranslation(insertionPoint);
        bref->setBlockTransform(trans);

        // Append it to modelspace
        AcDbObjectId BrefID;
        es = PostToDb(db, BrefID, bref);
        if (!es)
                bref->close();
        else
                delete bref;
}[/code]

 

 

 

 

arx创建块源码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|中国膜结构网|中国膜结构协会|进口膜材|国产膜材|ETFE|PVDF|PTFE|设计|施工|安装|车棚|看台|污水池|中国膜结构网_中国空间膜结构协会

GMT+8, 2024-11-1 15:22 , Processed in 0.133745 second(s), 24 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表