.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Create block without openning a dialog box

6 REPLIES 6
Reply
Message 1 of 7
yalda_gh
653 Views, 6 Replies

Create block without openning a dialog box

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

 

 

6 REPLIES 6
Message 2 of 7
Hallex
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
Message 3 of 7
yalda_gh
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

 

 

 

 

Message 4 of 7
Hallex
in reply to: yalda_gh

What is your Acad release (version) ?

Try this line instead:

 

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

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 5 of 7
yalda_gh
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

Message 6 of 7
Hallex
in reply to: yalda_gh

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

will be help for you,

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 7 of 7
yalda_gh
in reply to: Hallex

No matter, I can use Acad 2010.

 

Could you help me,please?

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost