.NET

Reply
Member
yalda_gh
Posts: 4
Registered: ‎07-09-2012
Message 1 of 7 (271 Views)

Create block without openning a dialog box

271 Views, 6 Replies
07-09-2012 11:25 PM

Hi,

 

I've written a program with c#.net for selecting objects and creating block without openning a window in autocad. But when I run its dll in autocad, it makes a block including many of the selected objects by me/user in one block(Please see attachments)

 

Please help me to solve this problem,

 

Thank you

 

 

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 7 (264 Views)

Re: Create block without openning a dialog box

07-10-2012 12:25 AM in reply to: yalda_gh

Try this code this isn't mine but seems to do your work

 

        #region "Placing block correctly on UCS"
        [CommandMethod("BlockKeepObjects", CommandFlags.UsePickSet)]
        public void BlockKeepObjectsCommand()
        {
            BlockCommand(true);
        }
        [CommandMethod("BlockReplaceObjects", CommandFlags.UsePickSet)]
        public void BlockReplaceObjectsCommand()
        {
            BlockCommand(false);
        }

        void BlockCommand(bool keepObjects)
        {
            // written by Thoersten Kaefer
            Database db = acadApp.DocumentManager.MdiActiveDocument.Database;
            Editor ed = acadApp.DocumentManager.MdiActiveDocument.Editor;

            PromptSelectionResult psr = ed.GetSelection();
            if (psr.Status != PromptStatus.OK) return;
            PromptStringOptions pso =
                new PromptStringOptions(
                    "Enter Block Name: ")
                {
                    AllowSpaces = true
                };
            PromptResult pr = ed.GetString(pso);
            if (pr.Status != PromptStatus.OK) return;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                bool nameOk = false;
                try
                {
                    SymbolUtilityServices.ValidateSymbolName(pr.StringResult, false);
                    if (bt.Has(pr.StringResult))
                        ed.WriteMessage("\nBlock \"{0}\" already exists. ", pr.StringResult);
                    else
                        nameOk = true;
                }
                catch
                {
                    ed.WriteMessage("\nInvalid block name: \"{0}\". ", pr.StringResult);
                }
                if (!nameOk) return;
                PromptPointOptions ppo =
                    new PromptPointOptions(
                        "\nPick Insertion Point");
                PromptPointResult ppr = ed.GetPoint(ppo);
                if (ppr.Status != PromptStatus.OK) return;

                // Create BlockTableRecord
                Matrix3d ucs2wcs = ed.CurrentUserCoordinateSystem;
                Matrix3d wcs2ucs = ucs2wcs.Inverse();
                using (BlockTableRecord btr =
                            new BlockTableRecord()
                            {
                                Origin = ppr.Value,
                                Name = pr.StringResult,
                                Annotative = AnnotativeStates.True
                            })
                {
                    // Add BlockTableRecord to Database
                    bt.UpgradeOpen();
                    bt.Add(btr);
                    tr.AddNewlyCreatedDBObject(btr, true);

                    // Populate newly created BlockTableRecord with selected objects
                    ObjectIdCollection oidc = new ObjectIdCollection(psr.Value.GetObjectIds());
                    if (keepObjects)
                        db.DeepCloneObjects(oidc, btr.ObjectId, new IdMapping(), false);
                    else
                        btr.AssumeOwnershipOf(oidc);

                    // Transform objects now in block from WCS to UCS
                    // and collect attributes
                    System.Collections.Generic.List<AttributeDefinition> attdefs =
                        new System.Collections.Generic.List<AttributeDefinition>();
                    foreach (ObjectId oid in btr)
                    {
                        Entity ent = (Entity)tr.GetObject(oid, OpenMode.ForWrite);
                        ent.TransformBy(wcs2ucs);
                        AttributeDefinition ad = ent as AttributeDefinition;
                        if (ad != null && !ad.Constant) attdefs.Add(ad);
                    }
                    // Create BlockReference 
                    using (BlockReference br = new BlockReference(Point3d.Origin, bt[pr.StringResult]))
                    {
                        br.TransformBy(ucs2wcs * Matrix3d.Displacement(ppr.Value.GetAsVector()));
                        var occ = db.ObjectContextManager.GetContextCollection("ACDB_ANNOTATIONSCALES");
                        if (occ.HasContext(db.Cannoscale.Name))
                            Autodesk.AutoCAD.Internal.ObjectContexts.AddContext(
                                br, occ.GetContext(db.Cannoscale.Name));

                        // Add BlockReference to current space
                        BlockTableRecord cspace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                        cspace.AppendEntity(br);
                        tr.AddNewlyCreatedDBObject(br, true);

                        // Add attributes to BlockReference, if any
                        foreach (AttributeDefinition ad in attdefs)
                            using (AttributeReference attref = new AttributeReference())
                            {
                                attref.SetAttributeFromBlock(ad, br.BlockTransform);
                                if (ad.IsMTextAttributeDefinition)
                                    attref.UpdateMTextAttribute();
                                attref.AdjustAlignment(db);
                                br.AttributeCollection.AppendAttribute(attref);
                                tr.AddNewlyCreatedDBObject(attref, true);
                            }
                    }
                }
                tr.Commit();
            }
        }

        #endregion

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Member
yalda_gh
Posts: 4
Registered: ‎07-09-2012
Message 3 of 7 (226 Views)

Re: Create block without openning a dialog box

07-11-2012 01:15 AM in reply to: yalda_gh

Thank you so much,

 

But I run this program and was confronted with a run time error in line* :

 

 

var occ = db.ObjectContextManager.GetContextCollection("ACDB​_ANNOTATIONSCALES");

if (occ.HasContext(db.Cannoscale.Name))

*    Autodesk.AutoCAD.Internal.ObjectContexts.AddContext(br, occ.GetContext(db.Cannoscale.Name));

 

 

What should I do?

 

Thanks

 

 

 

 

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 4 of 7 (215 Views)

Re: Create block without openning a dialog box

07-11-2012 07:17 AM in reply to: yalda_gh

What is your Acad release (version) ?

Try this line instead:

 

ObjectContextCollection occ = db.ObjectContextManager.GetContextCollection("ACDB_ANNOTATIONSCALES");

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Member
yalda_gh
Posts: 4
Registered: ‎07-09-2012
Message 5 of 7 (192 Views)

Re: Create block without openning a dialog box

07-12-2012 01:32 AM in reply to: yalda_gh

My Acad version is 2012.

 

My problem hasn't been solved. In line:

 

ObjectContextCollection occ = db.ObjectContextManager.GetContextCollection("ACDB​_ANNOTATIONSCALES");

 

"ACDB​_ANNOTATIONSCALES" is a collection name, but I don't know where that collection has been defined and where it has gotten a value. 

 

In run time, occ is null.

 

Please let me know how can I solve this problem?

 

Thank you

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 6 of 7 (190 Views)

Re: Create block without openning a dialog box

07-12-2012 02:33 AM in reply to: yalda_gh

Sorry I just have A2010, pehaps somebody else from this NG

will be help for you,

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Member
yalda_gh
Posts: 4
Registered: ‎07-09-2012
Message 7 of 7 (171 Views)

Re: Create block without openning a dialog box

07-12-2012 01:55 PM in reply to: Hallex

No matter, I can use Acad 2010.

 

Could you help me,please?

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!