Author Topic: Clone entity in block reference  (Read 261 times)

0 Members and 1 Guest are viewing this topic.

daboho

  • Mosquito
  • Posts: 15
Clone entity in block reference
« on: August 22, 2021, 05:43:48 AM »
first step i am insert block reference to model space,after that copy paste manual block reference to another point
example insert block reference in point3d (0,0,0) manualy
copy become 4 blockrefence to another point
problem is while run this code ,only clone one entity in block reference see picture (green is only one is clone)
i want clone all block reference has selected ( in this picture white color,i want also to be clone)
this is my code what is wrong
Code: [Select]
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 
    {
        [CommandMethod("xx")]
        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<KeyValuePair<ObjectId, double>>();
            var pso2 = new PromptSelectionOptions(); SelectionFilter sfilter = null;
     
            //SELECT FOR BLOCK REFERENCE
            PromptSelectionResult ss2 = null;
            pso2.MessageForAdding = "\nSelect Block Reference <Click After Select>:";
            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 (ky < crv1.Area)
                                    {
                                        ky = crv1.Area;
                                        myid = crv1.ObjectId;
                                        curve = crv1;
                                    }
                                }
                            }

                        }
                        if (ky > 0)
                        {
                            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();
           
            }

        }



    }
}


gile

  • Water Moccasin
  • Posts: 2283
  • Marseille, France
Re: Clone entity in block reference
« Reply #1 on: August 22, 2021, 10:55:47 AM »
Hi,

Try to make things as simple as possible. Here's your code after removing useless statements.
Code - C#: [Select]
  1.         [CommandMethod("xx")]
  2.         public static void xxCommand()
  3.         {
  4.  
  5.             var doc = Application.DocumentManager.MdiActiveDocument;
  6.             var db = doc.Database;
  7.             var ed = doc.Editor;
  8.  
  9.             //SELECT FOR BLOCK REFERENCE
  10.             var pso = new PromptSelectionOptions();
  11.             pso.MessageForAdding = "\nSelect Block Reference <Click After Select>:";
  12.             var filter = new SelectionFilter(new[] { new TypedValue(0, "INSERT") });
  13.             var psr = ed.GetSelection(pso, filter);
  14.             if (psr.Status != PromptStatus.OK)
  15.             {
  16.                 Application.ShowAlertDialog("None block reference selected !");
  17.                 return;
  18.             }
  19.             using (Transaction tr = db.TransactionManager.StartTransaction())
  20.             {
  21.                 var btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
  22.                 foreach (ObjectId id in psr.Value.GetObjectIds())
  23.                 {
  24.                     var br = (BlockReference)id.GetObject(OpenMode.ForRead);
  25.                     var objCol = new DBObjectCollection();
  26.                     br.Explode(objCol);
  27.                     foreach (DBObject obj in objCol)
  28.                     {
  29.                         if (obj is Curve curve && 0.0 < curve.Area)
  30.                         {
  31.                             curve.ColorIndex = 3;
  32.                             btr.AppendEntity(curve);
  33.                             tr.AddNewlyCreatedDBObject(curve, true);
  34.                         }
  35.                         else
  36.                         {
  37.                             obj.Dispose();
  38.                         }
  39.                     }
  40.                 }
  41.                 ed.WriteMessage("\ncount block reference " + psr.Value.Count.ToString());
  42.                 tr.Commit();
  43.             }
  44.         }
Speaking English as a French Frog

daboho

  • Mosquito
  • Posts: 15
Re: Clone entity in block reference
« Reply #2 on: August 22, 2021, 11:57:37 AM »
thank has respon my question
actualy i am only want to clone entity in blockreference
where is entity is closed and area is max from other entity
your code all entity to clone
how to change your code
see picture green colour is what i wanted (entity to clone)
i want only cloned not explode all entity
« Last Edit: August 22, 2021, 12:03:00 PM by daboho »

daboho

  • Mosquito
  • Posts: 15
Re: Clone entity in block reference
« Reply #3 on: August 22, 2021, 12:15:50 PM »
@Gile i has sucses for column using make region
problem if not column like door,etc
see picture
in code picture number 1,region is in green colour
should be like picture number 2 (where region is outside or rectangle is bigger/max area where area should be in picture 2 red borders)
how to change my code
Code: [Select]
[CommandMethod("bref1")]
        public static void beref()
        {
            var doc = acApp.DocumentManager.MdiActiveDocument;
            var ed = doc.Editor;
            Database db = acApp.DocumentManager.MdiActiveDocument.Database;
            var objCol = new DBObjectCollection();
            var ListPoints = new List<KeyValuePair<Curve, double>>();
            var ss2 = ed.GetSelection();
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                ObjectId ModelSpaceId = SymbolUtilityServices.GetBlockModelSpaceId(db);
                BlockTableRecord model = trans.GetObject(ModelSpaceId,OpenMode.ForWrite) as BlockTableRecord;
                foreach (ObjectId id in ss2.Value.GetObjectIds())
                {
                    var ent = (Entity)id.GetObject(OpenMode.ForRead);
                    if (ent is BlockReference)
                    {
                        var br = ent.ObjectId.GetObject(OpenMode.ForRead) as BlockReference;
                        Extents3d e3d;
                        try
                        {
                            e3d = br.GeometryExtentsBestFit();
                            Point3d cnt = new Point3d  (((e3d.MinPoint.X + e3d.MaxPoint.X) / 2)-0.05, (e3d.MinPoint.Y + e3d.MaxPoint.Y) / 2, (e3d.MinPoint.Z + e3d.MaxPoint.Z) / 2);
                            DBObjectCollection collection =
                            ed.TraceBoundary(cnt, true);
                            foreach (DBObject obj in collection)
                            {
                                Entity ent1 = obj as Entity;
                                if (ent1 != null)
                                {
                                    //make the color as red.
                                    ent1.ColorIndex = 3;
                                    model.AppendEntity(ent1);
                                    trans.AddNewlyCreatedDBObject(ent1, true);
                                }

                            }
                        }
                        catch (System.Exception)
                        {
                            e3d = br.GeometricExtents;
                        }
                        //ed.WriteMessage("\n point extend blockref " + e3d.ToString());
                    }

                }
                trans.Commit();
            }
        }
« Last Edit: August 22, 2021, 12:20:41 PM by daboho »

daboho

  • Mosquito
  • Posts: 15
Re: Clone entity in block reference
« Reply #4 on: August 22, 2021, 03:31:52 PM »
@gile thanks very2 match, for your help this has solved
Code: [Select]
[CommandMethod("xx")]
        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 <Click After Select>:";
            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<DBObject>()
                        .Select(p => ((Curve)tr.GetObject(p.ObjectId, OpenMode.ForRead)))
                        .Where(x => x.Area > 0)
                        .OrderByDescending(z => z.Area);

                    var curve = objCol[0] as Curve;
                    AddLayer("Layerhelper", 0, 82,false);
                    curve.ColorIndex = 253;
                    btr.AppendEntity(curve);
                    tr.AddNewlyCreatedDBObject(curve, true);
                }
                tr.Commit();
                AddLayer("0", 0, 0, false);
            }
        }