Finding the Coordinates of the insertion point C# (.NET)

Finding the Coordinates of the insertion point C# (.NET)

Anonymous
Not applicable
3,315 Views
3 Replies
Message 1 of 4

Finding the Coordinates of the insertion point C# (.NET)

Anonymous
Not applicable

Hi guys i'm currently doing a simple feature where I get a program to read then export the Name and Location of the insertion point to a .txt file.

I've managed to do the name of the block but I cant figure out for the life of me how to find the coordinates for the insertion point. Here is what I have so far any help would be appreciated.

 

using System;
using System.IO;
using System.Security;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Application = Autodesk.AutoCAD.ApplicationServices.Core.Application;

namespace Extracting_Attributes
{
    public class GettingAtrributes
    {
        /// <exception cref="DirectoryNotFoundException">The specified path is invalid (for example, it is on an unmapped drive). </exception>
        /// <exception cref="SecurityException">The caller does not have the required permission. </exception>
        [CommandMethod("LISTATT")]
        public void ListAttributes()

        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

            Database db = HostApplicationServices.WorkingDatabase;

            Transaction tr = db.TransactionManager.StartTransaction();

            // Start the transaction

            try

            {
                // Build a filter list so that only block references are selected

                var filList = new TypedValue[1] {new TypedValue((Int32) DxfCode.Start, "INSERT")};

                var filter = new SelectionFilter(filList);

                var opts = new PromptSelectionOptions();

                opts.MessageForAdding = "Select block references: ";

                PromptSelectionResult res = ed.GetSelection(opts, filter);

                // Do nothing if selection is unsuccessful

                if (res.Status != PromptStatus.OK)

                    return;

                SelectionSet selSet = res.Value;

                ObjectId[] idArray = selSet.GetObjectIds();

                foreach (ObjectId blkId in idArray)

                {
                    var blkRef = (BlockReference) tr.GetObject(blkId, OpenMode.ForRead);


                    var btr = (BlockTableRecord) tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead);

                    ed.WriteMessage("\nBlock: " + btr.Name);
                    System.IO.File.WriteAllText(@"C:\Users\robert.parsons\Documents\Attributes.txt", btr.Name);

                    btr.Dispose();

                    AttributeCollection attCol = blkRef.AttributeCollection;

                    foreach (ObjectId attId in attCol)

                    {
                        var attRef = (AttributeReference) tr.GetObject(attId, OpenMode.ForRead);

                        String str = "\n  Attribute Tag: " + attRef.Tag + "\n    Attribute String: " + attRef.TextString;
                        System.IO.File.WriteAllText(@"C:\Users\robert.parsons\Documents\Collection of Attributes.txt", str);
                        
                        
                        ed.WriteMessage(str);
                    }
                }

                tr.Commit();
            }

            catch (Autodesk.AutoCAD.Runtime.Exception ex)

            {
                ed.WriteMessage("Exception: " + ex.Message);
            }
            tr.Dispose();
        }
    }
}    

 

0 Likes
Accepted solutions (1)
3,316 Views
3 Replies
Replies (3)
Message 2 of 4

_gile
Consultant
Consultant
Accepted solution

Hi,

 

You can get the block reference insertion point with its Position property:

 

blkRef.Position

But it seems to me there's something curious the way you write the txt file.

For each selected block reference, the C:\Users\robert.parsons\Documents\Attributes.txt file is overwritten.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 4

_gile
Consultant
Consultant

Perhaps you can get some inspiration from this:

 

        [CommandMethod("LISTATT")]
        public void ListAttributes()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            var filter = new SelectionFilter(new[] { new TypedValue(0, "INSERT") });
            var opts = new PromptSelectionOptions();
            opts.MessageForAdding = "Select block references: ";
            var res = ed.GetSelection(opts, filter);
            if (res.Status != PromptStatus.OK)
                return;
            string str = "";
            using (var tr = db.TransactionManager.StartTransaction())
            {
                foreach (SelectedObject so in res.Value)
                {
                    var br = (BlockReference)tr.GetObject(so.ObjectId, OpenMode.ForRead);
                    str += $"{br.Name} {br.Position:0.00}\r\n";
                    if (br.AttributeCollection.Count > 0)
                    {
                        str += "Attributes:\r\n";
                        foreach (ObjectId id in br.AttributeCollection)
                        {
                            var att = (AttributeReference)id.GetObject(OpenMode.ForRead);
                            str += $"\tTag: {att.Tag} Text: {att.TextString}\r\n";
                        }
                    }
str += "\r\n"; } tr.Commit(); } ed.WriteMessage(str); System.IO.File.WriteAllText(@"C:\Users\robert.parsons\Documents\Attributes.txt", str);; }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 4

Anonymous
Not applicable

Thank I did change it to WriteAllLines

0 Likes