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

Creating MLeader using Builtin Blocks.

11 REPLIES 11
SOLVED
Reply
Message 1 of 12
mnav
2174 Views, 11 Replies

Creating MLeader using Builtin Blocks.

Hi 


  I want to use built in source blocks like Box, Circle, detail callout,etc as blockcontent property for MLeader, how I can access them from drawing.

  Drawing BlockTable dont have any blocks with such names i get exception when i try to access block.

  i am using autocad 2012 with c#.

 

         var table = Tx.GetObject(db.BlockTableId,OpenMode.ForRead) as BlockTable;

         var leader = new MLeader();

        leader.SetDatabaseDefaults();

        leader.ContentType = ContentType.BlockContent;

        leader.BlockContentId = table["Box"]; // Exception on this line

 

How i can access built in blocks is there any specific names or method for them?

 

 

thanks

11 REPLIES 11
Message 2 of 12
_gile
in reply to: mnav

Hi,

 

The BlockContentId refers to a BlockReference ObjectId, you have to insert the block before setting the Leader BlockContentId property with the newly inserted block ObjectId.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 12
mnav
in reply to: _gile

how we insert Mleader builtin blocks like Box?
Message 4 of 12
_gile
in reply to: mnav

Sorry, I confused MLeader with Leader

 

Did you try defining the MLeader geometry (Line and Vertices) before stting its contents:

 

var leader = new MLeader();
leader.SetDatabaseDefaults();
leader.AddLeader();
leader.AddLeaderLine(0);
leader.AddFirstVertex(0, startPoint);
leader.AddLastVertex(0, endPoint);
leader.ContentType = ContentType.BlockContent;
leader.BlockContentId = bt["Box"];

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 12
mnav
in reply to: _gile

Yes it give eKeyNotFound exception
Message 6 of 12
_gile
in reply to: mnav


mnav a écrit :
Yes it give eKeyNotFound exception

Does the block table contains the block "Box"? I assumed it had.

 

You'd check for this in your code:

 

if (table.Has("Box"))
{
    // make the leader here
}

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 7 of 12
mnav
in reply to: _gile

if i open new drawing and run the following code, i am unable to find any block name Box.

 

var activeDoc = Application.DocumentManager.MdiActiveDocument;

var db = activeDoc.Database;

using (var tran = db.TransactionManager.StartTransaction())

{

  var blockTable = tran.GetObject(db.BlockTableId, OpenMode.ForRead) asBlockTable;

  var mlead = newMLeader();

    mlead.SetDatabaseDefaults();

  var leaderNumber = mlead.AddLeader();

  var leaderLineNum = mlead.AddLeaderLine(leaderNumber);

    mlead.AddFirstVertex(leaderLineNum, startPoint);

    mlead.AddLastVertex(leaderLineNum, endPoint);

  if (blockTable.Has(("Box")))

    {

   mlead.ContentType = ContentType.BlockContent;

   mlead.BlockContentId = blockTable["Box"];

   ....

    }

  

  return;

 

}

 

how i can find builtin block Box.

 

Thanks

 

Message 8 of 12
_gile
in reply to: mnav

I do not undestand what you mean with "builtin block Box".

 

If the block "Box" isn't already in the drawing block table, you have to add it.

If the block exists as "Box.dwg" file in some place you know, you can add it to the block table using the Database.Insert() method with the hard coded path.

You can also try to find the "Block.dwg" file in the search paths with HostApplicationServices.FindFile() method.

These methods will fail if the block isn't found.

Another way which will never fail is to create the block table record "on the fly".

 

Here's a litlle sample which shows these different ways

 

                // check if the "Box" block already exists in the block table
                if (blockTable.Has("Box"))
                {
                    blockId = blockTable["Box"];
                }
                else
                {
                    // try to get "Box.dwg" from a known location
                    if (System.IO.File.Exists(@"F:\Block files\Box.dwg"))
                    {
                        // add the block to the block table
                        blockTable.UpgradeOpen();
                        using (var sourceDb = new Database())
                        {
                            sourceDb.ReadDwgFile(@"F:\Block files\Box.dwg", System.IO.FileShare.Read, false, null);
                            blockId = db.Insert("Box", sourceDb, true);
                        }
                    }
                    else
                    {
                        // try to find "Box.dwg" in the search paths
                        try
                        {
                            string blockPath = HostApplicationServices.Current.FindFile("Box.dwg", db, FindFileHint.Default);
                            // add the block to the block table
                            blockTable.UpgradeOpen();
                            using (var sourceDb = new Database())
                            {
                                sourceDb.ReadDwgFile(blockPath, System.IO.FileShare.Read, false, null);
                                blockId = db.Insert("Box", sourceDb, true);
                            }
                        }
                        // or build the block
                        catch
                        {
                            // create a new BlockTableRecord and add it to the bloc table
                            var btr = new BlockTableRecord();
                            btr.Name = "Box";
                            blockTable.UpgradeOpen();
                            blockId = blockTable.Add(btr);
                            tr.AddNewlyCreatedDBObject(btr, true);

                            // add some entities to the newly created BlockTableRecord
                            var circle = new Circle(Point3d.Origin, Vector3d.ZAxis, 2.0);
                            btr.AppendEntity(circle);
                            tr.AddNewlyCreatedDBObject(circle, true);
                            var pline = new Polyline(4);
                            pline.AddVertexAt(0, new Point2d(-2.0, -2.0), 0.0, 0.0, 0.0);
                            pline.AddVertexAt(1, new Point2d(2.0, -2.0), 0.0, 0.0, 0.0);
                            pline.AddVertexAt(2, new Point2d(2.0, 2.0), 0.0, 0.0, 0.0);
                            pline.AddVertexAt(3, new Point2d(-2.0, 2.0), 0.0, 0.0, 0.0);
                            pline.Closed = true;
                            btr.AppendEntity(pline);
                            tr.AddNewlyCreatedDBObject(pline, true);
                        }
                    }
                }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 9 of 12
mnav
in reply to: _gile

Hi Gilles

Thanks for your help.

 

By Builtin Blocks mean Box and Circle etc in Multileader style settings. i was trying to find a way to load those blocks programatically

or find file location for those blocks which we can use for block insertion.

 

 

thanks

 

Message 10 of 12
_gile
in reply to: mnav

The block you're looking for is named "_TagBox" (or "_TagCircle").

It seems to me these blocks do not already exist in AutoCAD resources and are created "on the fly" when needed.

I didn't find any other way to get them than using the MLeaderStyle dialog.

 

Anyway, you can do the same as AutoCAD and create you own block "on the fly" the first time you call the method and get it from the block table every next times.

 

using System;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;

[assembly: CommandClass(typeof(MLeaderSample.CommandMethods))]

namespace MLeaderSample
{
    public class CommandMethods
    {
        [CommandMethod("MLDR", CommandFlags.Modal)]
        public void MakeMleader()
        {
            var doc = AcAp.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            var opts = new PromptPointOptions("\nStart point: ");
            var ppr = ed.GetPoint(opts);
            if (ppr.Status != PromptStatus.OK)
                return;
            var startPoint = ppr.Value;

            opts.Message = "\nEnd point: ";
            opts.BasePoint = startPoint;
            opts.UseBasePoint = true;
            ppr = ed.GetPoint(opts);
            if (ppr.Status != PromptStatus.OK)
                return;
            var endPoint = ppr.Value;

            var ucsMat = ed.CurrentUserCoordinateSystem;
            var ucs = ucsMat.CoordinateSystem3d;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                var blockTable = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                ObjectId blockId, attDefId = ObjectId.Null;
                if (blockTable.Has("_Bubble"))
                {
                    blockId = blockTable["_Bubble"];
                    var btr = (BlockTableRecord)tr.GetObject(blockId, OpenMode.ForRead);
                    if (!btr.HasAttributeDefinitions)
                    {
                        ed.WriteMessage("\nWrong '_Bubble' block (none attribute).");
                        return;
                    }
                    foreach (var id in btr)
                    {
                        if (id.ObjectClass == RXClass.GetClass(typeof(AttributeDefinition)))
                        {
                            attDefId = id;
                            break;
                        }
                    }
                }
                else
                {
                    var btr = new BlockTableRecord();
                    btr.Name = "_Bubble";
                    blockTable.UpgradeOpen();
                    blockId = blockTable.Add(btr);
                    tr.AddNewlyCreatedDBObject(btr, true);
                    var circle = new Circle(Point3d.Origin, Vector3d.ZAxis, 4.0);
                    btr.AppendEntity(circle);
                    tr.AddNewlyCreatedDBObject(circle, true);
                    var attrib = new AttributeDefinition(
                        Point3d.Origin,
                        "NUMBERTAG",
                        "NUMBERTAG",
                        "Enter the tag number.",
                        db.Textstyle
                        );
                    attrib.Height = 3.0;
                    attrib.Justify = AttachmentPoint.MiddleCenter;
                    attrib.AlignmentPoint = Point3d.Origin;
                    attDefId = btr.AppendEntity(attrib);
                    tr.AddNewlyCreatedDBObject(attrib, true);
                }
                var mspace = (BlockTableRecord)tr.GetObject(
                    blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                var leader = new MLeader();
                leader.SetDatabaseDefaults();
                leader.AddLeader();
                leader.AddLeaderLine(0);
                leader.AddFirstVertex(0, startPoint);
                leader.AddLastVertex(0, endPoint);
                leader.SetDoglegLength(0, 4.0);
                leader.ContentType = ContentType.BlockContent;
                leader.BlockContentId = blockId;
                if (endPoint.X < startPoint.X)
                {
                    var disp = leader.BlockPosition.GetVectorTo(endPoint) * 2.0;
                    leader.MoveMLeader(disp, MoveType.MoveAllExceptArrowHeaderPoints);
                }
                leader.TransformBy(ucsMat);
                mspace.AppendEntity(leader);
                tr.AddNewlyCreatedDBObject(leader, true);
                db.TransactionManager.QueueForGraphicsFlush();
                var pr = ed.GetString("\nEnter the tag number: ");
                if (pr.Status == PromptStatus.OK)
                {
                    var attDef = (AttributeDefinition)tr.GetObject(attDefId, OpenMode.ForRead);
                    var attRef = new AttributeReference();
                    attRef.SetAttributeFromBlock(attDef, Matrix3d.Identity);
                    attRef.TextString = pr.StringResult;
                    leader.SetBlockAttribute(attDefId, attRef);
                }
                tr.Commit();
            }
        }
    }
}

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 11 of 12
mnav
in reply to: _gile

Hi Gilles

 

Thanks once again. i will try this way.

 

thanks

 

Message 12 of 12
Izhar_Azati
in reply to: mnav

Hello

The "BOX" control by:

leader.EnableFrameText = true;

 

		public static _AcDb.ObjectId DrawLeader( _AcDb.Database db, _AcDb.Transaction tr, _AcDb.BlockTableRecord ms, _AcDb.ObjectId layerId, _AcGe.Point3d pointArrow, _AcGe.Point3d pointForText, string txt, double textSize, _AcDb.ObjectId textStyleId ) {
			var dx = pointForText.X - pointArrow.X;
			// we ignor the Z !!
			ms.UpgradeOpen();
			var leader = new _AcDb.MLeader();
			leader.SetDatabaseDefaults();
			leader.LayerId = layerId;
			leader.ArrowSize = textSize * 2.0;
			leader.ContentType = _AcDb.ContentType.MTextContent;
			leader.TextStyleId = textStyleId;
			leader.TextHeight = textSize;
			leader.DoglegLength = 0.2;
			leader.LandingGap = 0.2;
			leader.EnableFrameText = true;
			var mText = new _AcDb.MText();
			mText.SetDatabaseDefaults();
			mText.TextStyleId = textStyleId;
			mText.Height = textSize;
			mText.LayerId = layerId;
			mText.Contents = txt;
			mText.Height = textSize;
			mText.Location = dx > 0.0 ? new _AcGe.Point3d( pointForText.X, pointForText.Y, 0.0 ) : new _AcGe.Point3d( pointForText.X - mText.ActualWidth, pointForText.Y, 0.0 );
			leader.MText = mText;
			if( dx > 0.0 ) {
				leader.TextAlignmentType = _AcDb.TextAlignmentType.LeftAlignment;
				leader.TextAttachmentDirection = _AcDb.TextAttachmentDirection.AttachmentHorizontal;
#if ARX_APP
				leader.SetTextAttachmentType( _AcDb.TextAttachmentType.AttachmentCenter, _AcDb.LeaderDirectionType.RightLeader );
#else
				leader.SetTextAttachmentType( _AcDb.TextAttachmentType.AttachmentMiddle, _AcDb.LeaderDirectionType.RightLeader );
#endif
			} else {
				leader.TextAlignmentType = _AcDb.TextAlignmentType.RightAlignment;
				leader.TextAttachmentDirection = _AcDb.TextAttachmentDirection.AttachmentHorizontal;
#if ARX_APP
				leader.SetTextAttachmentType( _AcDb.TextAttachmentType.AttachmentCenter, _AcDb.LeaderDirectionType.LeftLeader );
#else
				leader.SetTextAttachmentType( _AcDb.TextAttachmentType.AttachmentMiddle, _AcDb.LeaderDirectionType.LeftLeader );
#endif
			}
			var idx = leader.AddLeaderLine( new _AcGe.Point3d( pointArrow.X, pointArrow.Y, 0.0 ) );
			var objId = ms.AppendEntity( leader );
			tr.AddNewlyCreatedDBObject( leader, true );
			ms.DowngradeOpen();
			return objId;
		}

 

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