• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    *Expert Elite*
    Hallex
    Posts: 1,335
    Registered: ‎10-08-2008

    Re: Block-Functions (Select, Explode, etc.)

    04-17-2012 10:07 AM in reply to: klahie

    Sorry, try converted on C# instead

            public static void ApplyAttributes(Database db, Transaction tr, BlockReference bref)
            {
                BlockTableRecord btrec = tr.GetObject(bref.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
    
                if (btrec.HasAttributeDefinitions)
                {
                    Autodesk.AutoCAD.DatabaseServices.AttributeCollection atcoll = bref.AttributeCollection;
    
                    foreach (ObjectId subid in btrec)
                    {
                        Entity ent = (Entity)subid.GetObject(OpenMode.ForRead);
    
                        AttributeDefinition attDef = ent as AttributeDefinition;
    
                        if (attDef != null)
                        {
    
                            AttributeReference attRef = new AttributeReference();
    
                            attRef.SetDatabaseDefaults();
    
                            attRef.SetAttributeFromBlock(attDef, bref.BlockTransform);
    
                            attRef.Position = attDef.Position.TransformBy(bref.BlockTransform);
    
                            attRef.Tag = attDef.Tag;
    
                            attRef.TextString = attDef.TextString;
    
                            attRef.AdjustAlignment(db);
    
                            atcoll.AppendAttribute(attRef);
    
                            tr.AddNewlyCreatedDBObject(attRef, true);
    
                        }
                    }
                }
            }
            [CommandMethod("insBlock", CommandFlags.Modal | CommandFlags.UsePickSet)]
            public void TestInsert()
            {
                string blkname = "VT_VZ_VZ52_10a_ATT";
                //'<-- as per as on your drawing
                Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
                Editor ed = doc.Editor;
                Database db = doc.Database;
                try
                {
                    using (DocumentLock docloc = doc.LockDocument())
                    {
    
                        using (Transaction tr = db.TransactionManager.StartTransaction())
                        {
                            BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
    
                            if (!bt.Has(blkname))
                            {
                                ed.WriteMessage("\nBlock does not exists");
                                return;
                            }
    
                            PromptPointOptions pto = new PromptPointOptions("\nPick a block insertion point: ");
                            PromptPointResult ptres = ed.GetPoint(pto);
                            Point3d ipt = default(Point3d);
    
                            if (ptres.Status != PromptStatus.Cancel)
                            {
                                ipt = ptres.Value;
                            }
    
                            BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite, false);
                            BlockTableRecord blk = (BlockTableRecord)tr.GetObject(bt[blkname], OpenMode.ForRead, false);
                            BlockReference bref = new BlockReference(ipt, blk.ObjectId);
                            bref.BlockUnit = UnitsValue.Millimeters;
                            bref.Rotation = 0;
                            bref.ScaleFactors = new Scale3d(1000.0);
                            //'<-- as per as on your drawing
                            btr.AppendEntity(bref);
                            tr.AddNewlyCreatedDBObject(bref, true);
                            ApplyAttributes(db, tr, bref);
                            ed.Regen();
                            tr.Commit();
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    ed.WriteMessage(ex.Message);
                }
            }

     

    ~'J'~

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    *Expert Elite*
    chiefbraincloud
    Posts: 736
    Registered: ‎02-13-2008

    Re: Block-Functions (Select, Explode, etc.)

    04-17-2012 10:15 AM in reply to: Hallex

    I forgot all about "\n".

     

    Shame on me.  My lisp programming skills are getting too rusty.

    Dave O.                                                                                Sig-Logos32.png
    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,335
    Registered: ‎10-08-2008

    Re: Block-Functions (Select, Explode, etc.)

    04-17-2012 10:37 AM in reply to: chiefbraincloud

    But you know many other stuffs,

    Regards,

     

    Oleg

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,335
    Registered: ‎10-08-2008

    Re: Block-Functions (Select, Explode, etc.)

    04-17-2012 10:40 AM in reply to: klahie

    What about Copy-Paste, just a quick code almost not tested

     

            [CommandMethod("copypaste", CommandFlags.Modal | CommandFlags.UsePickSet)]
            public void CopyPaste()
            {
    
                Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
    
                Database db = doc.Database;
    
                Editor ed = doc.Editor;
    
                Matrix3d ucs = ed.CurrentUserCoordinateSystem;
                try
                {
    
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
    
                        PromptEntityOptions peo = new PromptEntityOptions("\nSelect a block  >>");
    
                        peo.SetRejectMessage("\nSelect block only >>");
    
                        peo.AddAllowedClass(typeof(BlockReference), false);
    
                        PromptEntityResult res;
    
                        res = ed.GetEntity(peo);
    
                        if (res.Status != PromptStatus.OK)
    
                            return;
    
                        Entity ent = (Entity)tr.GetObject(res.ObjectId, OpenMode.ForRead);
    
                        if (ent == null) return;
    
                        ObjectIdCollection ids = new ObjectIdCollection();
    
                        ids.Add(ent.ObjectId);
    
                        ObjectId chkId = ent.ObjectId;
    
                        BlockReference bref = ent as BlockReference;
    
                        Point3d pt = Point3d.Origin;
    
                        BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
    
                        BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
    
                        PromptPointOptions pto = new PromptPointOptions("\nPick a block insertion point: ");
    
                        PromptPointResult ptres = ed.GetPoint(pto);
    
                        Point3d ipt=Point3d.Origin;
    
                        if (ptres.Status != PromptStatus.Cancel)
                        {
                            ipt = ptres.Value;
                        }
    
                        using (IdMapping map = new IdMapping())
                        {
                            db.DeepCloneObjects(ids, bref.BlockId, map, false);
    
                            ObjectId clid = map[bref.ObjectId].Value;
    
                            BlockReference brefclone = (BlockReference)tr.GetObject(clid, OpenMode.ForWrite);
    
                            brefclone.Position =ipt;
    
                        }
    
                        tr.Commit();
    
                    }
                }
                catch (System.Exception ex)
                {
                    ed.WriteMessage(ex.Message);
                }
    
            }

     

     

    ~'J'~

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    Active Contributor
    Posts: 26
    Registered: ‎03-10-2012

    Re: Block-Functions (Select, Explode, etc.)

    04-17-2012 10:46 AM in reply to: chiefbraincloud

    EDIT: Wow, wow, wow, I was reading the first page and didn't see the second page :smileylol:

     

    That solved the second problem! :smileyhappy:

     

    How should I use chr(13)?

     

    PromptPointOptions pto = new PromptPointOptions(chr(13) + "Pick a block insertion point: ");

     gives the same problem

     

    Greets

    Klaus

    Please use plain text.
    *Expert Elite*
    chiefbraincloud
    Posts: 736
    Registered: ‎02-13-2008

    Re: Block-Functions (Select, Explode, etc.)

    04-17-2012 10:53 AM in reply to: klahie

    Well, I am surprised the chr function is not automatically in c#, but anyway, Oleg's code reminded me of the appropriate way to send a newline in c# (and lisp)  use "\nPick a block insertion point: "

    Dave O.                                                                                Sig-Logos32.png
    Please use plain text.
    Active Contributor
    Posts: 26
    Registered: ‎03-10-2012

    Re: Block-Functions (Select, Explode, etc.)

    04-17-2012 11:16 AM in reply to: chiefbraincloud

    :smileyvery-happy: That's great!

     

    The block is pasted dreamlike. :smileyhappy:

     

    BUT: When I refresh my blocklist, I havn't got a new entry for the pasted block. That means, in my list is only the old entry. 

     

    public void setBlockList()
    {
       BlockList = new List<String>();
       using (tr = db.TransactionManager.StartTransaction())
       {
          BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
          foreach (ObjectId objId in bt)
          {
    
             BlockTableRecord btr = (BlockTableRecord)tr.GetObject(objId, OpenMode.ForRead);
             if (isAttDef(btr))
             {
                BlockList.Add(btr.Name);
             }
          }
       }
    }

     That is my Blocklist-Function.

     

    Do you know why it fails?

     

    Greets

    Klaus

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,335
    Registered: ‎10-08-2008

    Re: Block-Functions (Select, Explode, etc.)

    04-17-2012 11:55 AM in reply to: klahie

    I didn't see nothing wrong with your code

    Perhaps you might be want to refresh BlockList first

    Something like:

            public void setBlockList()
            {
              List<String>  BlockList = new List<String>();
              Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
              Database db = doc.Database;
              Editor ed = doc.Editor;
                using (Transaction tr = db.TransactionManager.StartTransaction())
                { 
                    BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                    foreach (ObjectId objId in bt)
                    {
                        BlockTableRecord btr = (BlockTableRecord)tr.GetObject(objId, OpenMode.ForRead);
                        if (btr.HasAttributeDefinitions)
                       // if (isAttDef(btr))
                        { 
                            BlockList.Add(btr.Name); 
                        } 
                    }
                }
                foreach (string bname in BlockList)
                {
                    ed.WriteMessage("\n{0}", bname);
                }
    
            }

     

    ~'J'~

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    Active Contributor
    Posts: 26
    Registered: ‎03-10-2012

    Re: Block-Functions (Select, Explode, etc.)

    04-17-2012 12:13 PM in reply to: Hallex

    I call my setBlockList()-Function direkt after the Commit. No Copy-Blockname..... :smileyindifferent: 

     

    In the commandline (

    ed.WriteMessage("\n{0}", bname);

    ) is only one blockname too, the blockname of blk (using the function you posted).

     

    Greets

    Klaus

    Please use plain text.
    *Expert Elite*
    chiefbraincloud
    Posts: 736
    Registered: ‎02-13-2008

    Re: Block-Functions (Select, Explode, etc.)

    04-17-2012 03:15 PM in reply to: klahie

    I don't think it is failing.

     

    I think you still don't quite understand the difference between a BlockReference and a BlockTableRecord.

     

    You are listing BlockTableRecords in your Block List.  When you copy/paste, you are creating a new BlockReference pointing to the same BlockTableRecord as the original copied BlockReference.  This does not create a new BlockTableRecord.

     

    There can only be one BlockTableRecord with a given name, but there can (and usually will be) many BlockReferences that point to same BlockTableRecord.  That is really the point of Blocks, that you store the definition of the block (BlockTableRecord) once in the BlockTable, and then you create multiple instances of the block (BlockReference) as many times as needed in the Model or Paper spaces.  This saves space in the database and keeps the drawing size smaller, because the BlockReferences just contain a pointer to the BlockTableRecord, instead of containing all of the information for the lines, polylines and whatever other geometry they might contain.

     

    I'm still looking for the old post of mine that explains the difference in greater detail...

    Dave O.                                                                                Sig-Logos32.png
    Please use plain text.