Add Block reference to Autocad Layout

Add Block reference to Autocad Layout

Anonymous
Not applicable
2,415 Views
5 Replies
Message 1 of 6

Add Block reference to Autocad Layout

Anonymous
Not applicable

Hello,

 

I am New to CAD Customization. I want to add Block References to Autocad Layout. I want to Add first Block Reference in top Left Position of Layout and if there is Space remaining on the layout then add another block in the same layout at a point below it.

 

Thanking In advance

Regards.

0 Likes
2,416 Views
5 Replies
Replies (5)
Message 2 of 6

_gile
Consultant
Consultant

Hi,

 

If you are a complete newby with .NET programming, you'd first learn .NET and C# basics before try to customize AutoCAD.

If you already have some .NET basics knowledge but completely new to AutoCAD .NET, you'd start with simpler tasks, fistly learn how to create an AutoCAD .NET project from scratch.

If you already have some AutoCAD .NET basics and want some help to learn how to di this, you should post what you have done so far.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 6

BKSpurgeon
Collaborator
Collaborator

Welcome!

 

I concur with my estimable colleague and would also add the following:

 

http://usa.autodesk.com/adsk/servlet/index?siteID=123112&id=1911627

 

http://help.autodesk.com/view/ACD/2017/FRA/?guid=GUID-BA686431-C8BF-49F2-946E-9CEB2F7AE4FA

 

https://www.theswamp.org/index.php?topic=32381.0

 

Reading between the lines, I"m afraid there are no easy short cuts: 

 

Here is the code which shows you how to create and insert block references:

 

http://docs.autodesk.com/ACD/2014/ENU/index.html?url=files/GUID-2656E245-6EAA-41A3-ABE9-742868182821...

 

This will give you some ideas:

 

  1. You'd want to determine the geometric extents of your existing block reference and then you'd want to test if the extents of the newblock will still fit in the drawing and then decide whether to insert it or not. (That's how I'd approach the problem).

 

The following code will give you some ideas of how to insert block references: 

 

WARNING: UNTESTED:

 

namespace PracticePlugins
{

    // This class is instantiated by AutoCAD for each document when
    // a command is called by the user the first time in the context
    // of a given document. In other words, non static data in this class
    // is implicitly per-document!
    public class MyCommands
    {
        // The CommandMethod attribute can be applied to any public  member 
        // function of any public class.
        // The function should take no arguments and return nothing.
        // If the method is an intance member then the enclosing class is 
        // intantiated for each document. If the member is a static member then
        // the enclosing class is NOT intantiated.
        //
        // NOTE: CommandMethod has overloads where you can provide helpid and
        // context menu.

        // Modal Command with localized name
        
        [CommandMethod("InsertBlockReference")]
        public static void DisplayLayerNames3()
        {
            Point3d blockReferencePosition = InsertBlockReferenceHelpers.GetPoint("\n Please choose insertion point for block reference");

            // obviously you'll add in the name of the block in place of the "string" below:
            InsertBlockReferenceHelpers.InsertBlockReference("NameOfBlock", blockReferencePosition);
        }
    }
    

    public static class InsertBlockReferenceHelpers
    {
        static public void InsertBlockReference(string blockName, Point3d insertionPoint)
        {
            Database db = Application.DocumentManager.MdiActiveDocument.Database;
            Document doc = Application.DocumentManager.MdiActiveDocument;
            using (DocumentLock docLock = doc.LockDocument())
            {
                using (Transaction myT = db.TransactionManager.StartTransaction())
                {
                    //Get the block definition "Check".

                    BlockTable bt =
                        db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
                    BlockTableRecord blockDef =
                      bt[blockName].GetObject(OpenMode.ForRead) as BlockTableRecord;
                    //Also open modelspace - we'll be adding our BlockReference to it
                    BlockTableRecord ms =
                      bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite)
                                                              as BlockTableRecord;
                    //Create new BlockReference, and link it to our block definition

                    using (BlockReference blockRef =
                            new BlockReference(insertionPoint, blockDef.ObjectId))
                    {
                        //Add the block reference to modelspace
                        ms.AppendEntity(blockRef);
                        myT.AddNewlyCreatedDBObject(blockRef, true);
                        //Iterate block definition to find all non-constant
                        // AttributeDefinitions
                        foreach (ObjectId id in blockDef)
                        {
                            DBObject obj = id.GetObject(OpenMode.ForRead);
                            AttributeDefinition attDef = obj as AttributeDefinition;
                            if ((attDef != null) && (!attDef.Constant))
                            {
                                //This is a non-constant AttributeDefinition
                                //Create a new AttributeReference
                                using (AttributeReference attRef = new AttributeReference())
                                {
                                    attRef.SetAttributeFromBlock(attDef, blockRef.BlockTransform);
                                    // not setting the attribute value to anything.

                                    //Add the AttributeReference to the BlockReference
                                    blockRef.AttributeCollection.AppendAttribute(attRef);
                                    myT.AddNewlyCreatedDBObject(attRef, true);
                                }
                            }
                        }
                    }
                    //Our work here is done
                    myT.Commit();
                }
            }
        }

        public static Point3d GetPoint(string userInstructions)
        {
            // asks the user to select a point
            // throws an exception if he cancels
            // repeats till he gets it right
            // check if point is (0,0,0)

            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;

            PromptPointResult pPtRes = null;
            PromptPointOptions pPtOpts = new PromptPointOptions("");

            Point3d ptStart;

            // Prompt for the start point
            pPtOpts.Message = userInstructions;
            pPtRes = doc.Editor.GetPoint(pPtOpts);


            if (pPtRes.Status == PromptStatus.OK)
            {
                ptStart = pPtRes.Value;
            }
            else
            {
                throw new Autodesk.AutoCAD.Runtime.Exception(Autodesk.AutoCAD.Runtime.ErrorStatus.UserBreak, "Looks like there was an error when asked to click a point");
            }

            return ptStart;
        }
    }
}


 

 

 

good luck and i hope it helps.

 

rgds

 

Ben

0 Likes
Message 4 of 6

Anonymous
Not applicable

I have tried to create the block references. I ask the user for the insertion point for the block reference but the block reference is created at a distance from the specified point (please refer the attached Image). so when i try to insert the block reference in the specified layout the block reference is added outside the layout page.

0 Likes
Message 5 of 6

Anonymous
Not applicable

I have tried to create the block references. I ask the user for the insertion point for the block reference but the block reference is created at a distance from the specified point (please refer the attached Image). so when i try to insert the block reference in the specified layout the block reference is added outside the layout page.

0 Likes
Message 6 of 6

kerry_w_brown
Advisor
Advisor

 

Perhaps you should attach the drawing you are using

and the complete solution zipped so that anyone interested in this problem knows what you have actually done with the code.

 

 


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes