daboho 发表于 2021-8-22 05:43:48

克隆块引用中的实体

第一步,我将块引用插入到模型空间,在复制后,将手动块引用粘贴到另一个点
示例在点3d (0,0,0)中手动插入块引用
复制成为4个块引用到另一个点
问题是在运行此代码时,仅克隆块引用中的一个实体(见图片(绿色是唯一一个被克隆)
我想要克隆已选择的所有块引用(在此图片中为白色,我也想要被克隆)
这是我的代码,哪里出错了
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using Autodesk.AutoCAD.ApplicationServices;
using acApp = Autodesk.AutoCAD.ApplicationServices.Application;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using static latihanCAD.function;
namespace latihanCAD
{
    public class hapuslagi
    {
      
      public static void CreateHatchedBlock()
      {
            var doc = acApp.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            var ed = doc.Editor;
            var objCol = new DBObjectCollection();
            var ListPoints = new List>();
            var pso2 = new PromptSelectionOptions(); SelectionFilter sfilter = null;
   
            //SELECT FOR BLOCK REFERENCE
            PromptSelectionResult ss2 = null;
            pso2.MessageForAdding = "\nSelect Block Reference :";
            sfilter = new SelectionFilter(new[] { new TypedValue(0, "INSERT") });
            ss2 = ed.GetSelection(pso2, sfilter);
            if (ss2.Status != PromptStatus.OK)
            {
                MessageBox.Show("\nThere is not block reference to select :!");
                return;
            }
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                var btr = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                foreach (ObjectId id in ss2.Value.GetObjectIds())
                {
                  var ent = (Entity)id.GetObject(OpenMode.ForRead);
                  var br = ent as BlockReference;
                  if (ent is BlockReference)
                  {
                        double ky = 0.0;
                        ObjectId myid = ObjectId.Null;
                        Curve curve = null;
                        ent.Explode(objCol);
                        foreach (Object obj in objCol)
                        {
                            // ed.WriteMessage("\n{0}", obj.GetType());
                            if (obj is Curve)
                            {
                              var crv1 = obj as Curve;
                              if (crv1.Area > 0)
                              {
                                    if (ky0)
                        {
                            var ext1 = new Extents3d();
                            var cloneCurve = curve.Clone() as Entity;
                            cloneCurve.ColorIndex = 3;
                            ext1 = cloneCurve.GeometricExtents;
                            btr.AppendEntity(cloneCurve);
                            trans.AddNewlyCreatedDBObject(cloneCurve, true);
                            ed.WriteMessage("\nid clone curve " + cloneCurve.ObjectId.ToString());
                            cloneCurve.Dispose();
                        }
                  }
                }
                ed.WriteMessage("\ncount block reference " + ss2.Value.Count.ToString());
                trans.Commit();
            
            }
      }
    }
}



**** Hidden Message *****

gile 发表于 2021-8-22 10:55:47

嗨,试着让事情尽可能简单。这是删除无用语句后的代码, 。
公共静态 void xxCommand()。
{。

var doc = Application.DocumentManager.MdiActiveDocument;。
var db = doc.数据库;。
var ed = doc.编辑;。

选择块引用。
var pso = new PromptSelectionOptions();。
pso.消息为添加 = “\n选择块引用 :”;。
var filter = new SelectionFilter(new[] { new TypedValue(0, “INSERT”) });。
var psr = ed.GetSelection(pso, filter);。
如果 (psr.状态 != PromptStatus.OK)。
{。
Application.ShowAlertDialog(“None block reference selected !”);。
返回;。
}。
using (Transaction tr = db.TransactionManager.StartTransaction())。
{。
var btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;。
foreach (ObjectId id in psr.Value.GetObjectIds())。
{。
var br = (BlockReference)id.GetObject(OpenMode.ForRead);。
var objCol = new DBObjectCollection();。
br.Explode(objCol);。
foreach (DBObject obj in objCol)。
{。
if (obj is Curve curve && 0.0 。
{。
曲线,颜色索引 = 3;。
断续器追加实体(曲线);。
断续器AddNewlyCreatedDBObject(curve, true);。
}。
还。
{。
obj.处置();。
}。
}。
}。
编辑,WriteMessage(“\ncount block reference” + psr.Value.count.toString());。
断续器Commit();。
}。
}。

daboho 发表于 2021-8-22 11:57:37

谢谢已经回复了我的问题
,实际上我只想克隆实体在块引用
中是实体是关闭的,面积是从其他实体最大的
,你的代码所有实体如何克隆
如何更改你的代码
看到图片绿色是我想要的(实体到克隆)
我只想克隆不分解所有实体

daboho 发表于 2021-8-22 12:15:50

@gile非常感谢2 match,感谢您的帮助,这已经解决了

      public static void xxCommand()
      {
            var doc = acApp.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            //SELECT FOR BLOCK REFERENCE
            var pso = new PromptSelectionOptions();
            pso.MessageForAdding = "\nSelect Block Reference :";
            var filter = new SelectionFilter(new[] { new TypedValue(0, "INSERT") });
            var psr = ed.GetSelection(pso, filter);
            if (psr.Status != PromptStatus.OK)
            {
                acApp.ShowAlertDialog("None block reference selected !");
                return;
            }
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                var btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                foreach (ObjectId id in psr.Value.GetObjectIds())
                {
                  var br = (BlockReference)id.GetObject(OpenMode.ForRead);
                  var objCol = new DBObjectCollection();
                  br.Explode(objCol);
                  objCol
                        .Cast()
                        .Select(p => ((Curve)tr.GetObject(p.ObjectId, OpenMode.ForRead)))
                        .Where(x => x.Area > 0)
                        .OrderByDescending(z => z.Area);
                  var curve = objCol as Curve;
                  AddLayer("Layerhelper", 0, 82,false);
                  curve.ColorIndex = 253;
                  btr.AppendEntity(curve);
                  tr.AddNewlyCreatedDBObject(curve, true);
                }
                tr.Commit();
                AddLayer("0", 0, 0, false);
            }
      }
页: [1]
查看完整版本: 克隆块引用中的实体