Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从AutoCAD(C#)中的一组线创建实心块_C#_Autocad Plugin - Fatal编程技术网

从AutoCAD(C#)中的一组线创建实心块

从AutoCAD(C#)中的一组线创建实心块,c#,autocad-plugin,C#,Autocad Plugin,我正在尝试以编程方式在AutoCAD中绘制实体三维形状。我可以把形状画成线框。下面是我的代码当前绘制的内容 该对象是一组AutoCAD线,它们组合在一起形成一个组对象。我想要的是一个方法,它将一个组作为参数,并将该组转换为一个实心块引用。这是我的尝试 public static void DefineBlockFromGroup(Group groupToCreateBlockFrom, string blockName, Point3d origin, bool replaceChosenE

我正在尝试以编程方式在AutoCAD中绘制实体三维形状。我可以把形状画成线框。下面是我的代码当前绘制的内容

该对象是一组AutoCAD线,它们组合在一起形成一个组对象。我想要的是一个方法,它将一个组作为参数,并将该组转换为一个实心块引用。这是我的尝试

public static void DefineBlockFromGroup(Group groupToCreateBlockFrom, string blockName, Point3d origin, bool replaceChosenEntities = false)
{
    List<ObjectId> idsOfEntitiesInGroup = groupToCreateBlockFrom.GetAllEntityIds().ToList(); //Getting the entities that will be used to create the block
    CreateBlockDefinition(idsOfEntitiesInGroup, blockName, origin, replaceChosenEntities);
}

public static void CreateBlockDefinition(List<ObjectId> entities, string blockName, Point3d origin, bool replaceChosenEntities = false)
{
    BlockTableRecord record = new BlockTableRecord(); //Will become record of new block type
    ObjectId blockId = ObjectId.Null; //Will become id of new block type
    using (Transaction tr = _database.TransactionManager.StartTransaction())
    using (DocumentLock docLock = _activeDocument.LockDocument())
    {
        BlockTable bt = (BlockTable)tr.GetObject(_database.BlockTableId, OpenMode.ForRead); //Getting block table to put new record in
        bt.UpgradeOpen();
        record = new BlockTableRecord(); //New record will contain all entities which will make up the block
        record.Name = blockName; //Setting name of the block
        record.Origin = origin; //Setting origin point of the block
        bt.Add(record);
        tr.AddNewlyCreatedDBObject(record, true);
        blockId = bt[record.Name];

        tr.Commit();
    }

    //copy the select entities to block by using deepclone.
    ObjectIdCollection collection = new ObjectIdCollection(entities.ToArray());
    IdMapping mapping = new IdMapping();
    _database.DeepCloneObjects(collection, blockId, mapping, false);

    if (replaceChosenEntities)
    {
        using (Transaction tr = _database.TransactionManager.StartTransaction())
        using (DocumentLock docLock = _activeDocument.LockDocument())
        {
            Entity baseEntity = tr.GetObject(entities[0], OpenMode.ForWrite) as Entity;
            foreach (ObjectId id in entities)
            {
                EraseSingleObjectFromDrawingWithId(id);
            }
            DrawBlockFacingAPoint(record.Name, record.Origin, record.Origin, baseEntity.Layer, null);
            tr.Commit();
        }
    }
}

public static void EraseSingleObjectFromDrawingWithId(ObjectId idOfObjectToErase)
{
    // Start a transaction
    using (Transaction tr = _database.TransactionManager.StartTransaction())
    {
        DBObject objToErase = tr.GetObject(idOfObjectToErase, OpenMode.ForWrite);
        // Check to make sure a valid SelectedObject object was returned
        if (objToErase != null)
        {
            objToErase.Erase();
        }
        tr.Commit();
    }
}
public static BlockReference DrawBlockFacingAPoint(string name, Point3d position, Point3d direction, string layerToInsertOn, List<string> attributeValues = null, Distance xScale = null, Distance yScale = null, Distance zScale = null)
{
    LastPositionPoint = position;
    LastDirectionPoint = direction;
    //Creating default distances if null is passed for the scales
    if (xScale == null)
    {
        xScale = new Distance(DistanceType.Inch, 1);
    }
    if (yScale == null)
    {
        yScale = new Distance(DistanceType.Inch, 1);
    }
    if (zScale == null)
    {
        zScale = new Distance(DistanceType.Inch, 1);
    }

    ObjectId blkRecId = _generateBlockRecordId(name); //Generating ID for the block
    BlockReference blkRef = null; //Reference of the block that will be inserted

    using (Transaction tr = _database.TransactionManager.StartTransaction()) //Starting the transaction to insert the block into the drawing
    using (DocumentLock docLock = _activeDocument.LockDocument())
    {
        blkRef = new BlockReference(position, blkRecId); //Creating the block reference
        blkRef.SetDatabaseDefaults();
        blkRef.ScaleFactors = new Scale3d(xScale.Inches, yScale.Inches, zScale.Inches); //Changing the scales to what the programmer specifies
        blkRef.Layer = layerToInsertOn; //Assigning layer to draw the block on
        Point start = Point.MakePointWithInches(position.X, position.Y, position.Z); //Generating first AutoCAD point
        Point end = Point.MakePointWithInches(direction.X, direction.Y, direction.Z); //Generating second AutoCAD point
        GeometryClassLibrary.Line line = new GeometryClassLibrary.Line(start, end); //Creating a line to mirror a block over if necessary
        Angle a = new Angle(AngleType.Radian, 0); //Angle to rotate  the block by

        //Assigning angle based on direction point
        if (position.Y != direction.Y)
        {
            a = line.AngleBetween(GeometryClassLibrary.Line.XAxis);
        }
        if (direction.Y < position.Y)
        {
            blkRef.Rotation = a.Negate().GetValue(AngleType.Radian); //Allowing for rotations beyond 180 degrees
        }
        else
        {
            blkRef.Rotation = a.GetValue(AngleType.Radian);
        }

        BlockTableRecord blkTblRec = tr.GetObject(_database.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
        blkTblRec.AppendEntity(blkRef); //Adding block referece to the block table of the drawing
        tr.AddNewlyCreatedDBObject(blkRef, true); //Adding block to the drawing

        //Assigning attributes of the block
        BlockTableRecord btr = (BlockTableRecord)tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead);
        int attCounter = 0; //Counter to iterate through attributes
        foreach (ObjectId objId in btr) //Checking each item in the BlockReference's records
        {
            DBObject obj = objId.GetObject(OpenMode.ForRead);
            if (obj is AttributeDefinition) //If the object is an attribute, update it.
            {
                AttributeDefinition ad = obj as AttributeDefinition;
                AttributeReference ar = new AttributeReference();
                ar.SetAttributeFromBlock(ad, blkRef.BlockTransform);
                ar.Position = ad.Position.TransformBy(blkRef.BlockTransform);
                try
                {
                    ar.TextString = attributeValues.ElementAt(attCounter);
                }
                catch (ArgumentNullException)
                {
                    ar.TextString = "";
                }
                catch (ArgumentOutOfRangeException)
                {
                    ar.TextString = "";
                }
                attCounter++;
                blkRef.AttributeCollection.AppendAttribute(ar);
                tr.AddNewlyCreatedDBObject(ar, true);
            }
        }
        tr.Commit();
        return blkRef;
    }
}


private static ObjectId _generateBlockRecordId(string passedBlockName)
{
    Transaction tr = _database.TransactionManager.StartTransaction();
    DocumentLock docLock = _activeDocument.LockDocument();
    using (tr)
    using (docLock)
    {
        BlockTable blkTbl = tr.GetObject(_database.BlockTableId, OpenMode.ForRead) as BlockTable;
        ObjectId blkRecId = ObjectId.Null;
        if (blkTbl.Has(passedBlockName)) //Checking if the block exists
        {
            blkRecId = blkTbl[passedBlockName]; //If it does, getting the current id
        }
        else //If it doesn't exist create one
        {
            Database blkDb = new Database(false, true);
            blkDb.ReadDwgFile(passedBlockName + ".dwg", System.IO.FileShare.Read, true, ""); //Reading block source file
            _database.Insert(passedBlockName, blkDb, true);
            blkRecId = blkTbl[passedBlockName];
        }
        return blkRecId;
    }
}
public static void DefineBlockFromGroup(Group groupToCreateBlockFrom,string blockName,Point3d原点,bool replaceChosenEntities=false)
{
List idsOfEntitiesInGroup=groupToCreateBlockFrom.GetAllEntityIds().ToList();//获取将用于创建块的实体
CreateBlockDefinition(idsOfEntitiesInGroup、blockName、origin、replaceChosenEntities);
}
公共静态void CreateBlockDefinition(列出实体、字符串块名、Point3d原点、bool replaceChosenEntities=false)
{
BlockTableRecord record=new BlockTableRecord();//将成为新块类型的记录
ObjectId blockId=ObjectId.Null;//将成为新块类型的id
使用(Transaction tr=\u database.TransactionManager.StartTransaction())
使用(DocumentLock docLock=\u activeDocument.LockDocument())
{
BlockTable bt=(BlockTable)tr.GetObject(_database.BlockTableId,OpenMode.ForRead);//获取块表以放入新记录
bt.UpgradeOpen();
record=new BlockTableRecord();//新记录将包含构成块的所有实体
record.Name=blockName;//设置块的名称
record.Origin=Origin;//设置块的原点
bt.Add(记录);
tr.addNewlyCreatedBobObject(记录,真);
blockId=bt[record.Name];
tr.Commit();
}
//使用deepclone复制要阻止的选定实体。
ObjectdCollection集合=新的ObjectdCollection(entities.ToArray());
IdMapping=新的IdMapping();
_DeepCloneObjects(集合、块ID、映射、假);
如果(替换选择实体)
{
使用(Transaction tr=\u database.TransactionManager.StartTransaction())
使用(DocumentLock docLock=\u activeDocument.LockDocument())
{
实体baseEntity=tr.GetObject(实体[0],OpenMode.ForWrite)作为实体;
foreach(实体中的ObjectId)
{
从id为(id)的绘图中删除单个对象;
}
DrawBlockFacingAPoint(record.Name,record.Origin,record.Origin,baseEntity.Layer,null);
tr.Commit();
}
}
}
公共静态无效删除ID为的DrawingSingleObjects(ObjectId idOfObjectToErase)
{
//启动交易
使用(Transaction tr=\u database.TransactionManager.StartTransaction())
{
DBObject objToErase=tr.GetObject(idOfObjectToErase,OpenMode.ForWrite);
//检查以确保返回了有效的SelectedObject对象
if(objToErase!=null)
{
objToErase.Erase();
}
tr.Commit();
}
}
公共静态块引用DrawBlockFacingAPoint(字符串名称、点3D位置、点3D方向、字符串层插入、列表属性值=null、距离xScale=null、距离yScale=null、距离zScale=null)
{
LastPositionPoint=位置;
LastDirectionPoint=方向;
//如果为刻度传递null,则创建默认距离
if(xScale==null)
{
xScale=新距离(DistanceType.Inch,1);
}
if(yScale==null)
{
yScale=新距离(Distance type.Inch,1);
}
如果(zScale==null)
{
zScale=新距离(Distance type.Inch,1);
}
ObjectId blkRecId=\u generateBlockRecordId(名称);//为块生成ID
BlockReference blkRef=null;//将插入的块的引用
使用(事务tr=\u database.TransactionManager.StartTransaction())//启动事务以将块插入图形中
使用(DocumentLock docLock=\u activeDocument.LockDocument())
{
blkRef=新块引用(位置,blkRecId);//创建块引用
blkRef.SetDatabaseDefaults();
blkRef.ScaleFactors=new Scale3d(xScale.Inches,yScale.Inches,zScale.Inches);//将比例更改为程序员指定的比例
blkRef.Layer=layerToInsertOn;//指定要在其上绘制块的图层
点起点=点.MakePointWithInches(position.X,position.Y,position.Z);//生成第一个AutoCAD点
Point end=Point.MakePointWithInches(direction.X,direction.Y,direction.Z);//生成第二个AutoCAD点
GeometryClassLibrary.Line Line=新建GeometryClassLibrary.Line(开始,结束);//创建一条线以在必要时镜像块
角度a=新角度(AngleType.Radian,0);//旋转块的角度
//基于方向点指定角度
if(位置Y!=方向Y)
{
a=线之间的角度(GeometryClassLibrary.line.XAxis);
}
if(方向Y<位置Y)
{
blkRef.Rotation=a.Negate().GetValue(AngleType.Radian);//允许旋转超过180度
}
其他的
{
blkRef.Rotation=a.GetValue(角度类型弧度);
}
BlockTableRecord blkTblRec=tr.GetObject(_database.CurrentSpaceId,OpenMode.ForWrite)作为BlockTableRecord;
blkTblRec.AppendEntity(blkRef);//将块引用添加到图形的块表中
tr.AddNewlyCreatedDBObject(blkRef,true);//向图形中添加块
//指定块的属性
BlockTableRecord btr=(BlockTableRecord)tr.GetObject(blkRef.BlockTableRecord,OpenMode.ForRead);
int attCounter=0;//遍历属性的计数器
foreach(btr中的ObjectId objId)//检查块引用记录中的每个项
{
DBObject obj=objId.GetObject(OpenMode。