Author Topic: Create Mtext using Notepad for input string (A2010)  (Read 936 times)

0 Members and 1 Guest are viewing this topic.

Offline fixo

  • Full Member
  • ***
  • Posts: 135
  • Karma: +4/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Good
    • View Profile
Create Mtext using Notepad for input string (A2010)
« on: March 04, 2013, 10:00:47 AM »
     C#
Code: [Select]
        [System.Security.SuppressUnmanagedCodeSecurity]
        [DllImport("acad.exe", EntryPoint = "acedCmd", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
        extern static private int acedCmd(IntPtr resbuf);
        //-----------------------------------------------------------//
        // AutoCAD 2010 (eng.)
        [CommandMethod("mtx")]
        public void createMtext()
        {
            Clipboard.Clear();
            Document doc = doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Editor ed = doc.Editor;
            Database db = doc.Database;
            Transaction tr = db.TransactionManager.StartTransaction();
            Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("Add then \"Cut\" text from Notepad");
            System.Threading.Thread.Sleep(500);

            ResultBuffer rb = new ResultBuffer();

            rb.Add(new TypedValue((int)LispDataType.Text, "_startapp"));
            rb.Add(new TypedValue((int)LispDataType.Text, "notepad"));
            rb.Add(new TypedValue((int)LispDataType.Text, ""));// open Unnamed.txt
            rb.Add(new TypedValue((int)LispDataType.Text, "\n"));
            acedCmd(rb.UnmanagedObject);
            ed.Regen();
            using (tr)
            {   
                Point3d pt = ed.GetPoint("\nPick mtext location: ").Value;
                MText mtx = new MText();
                mtx.Location = pt;
                mtx.SetDatabaseDefaults();
                mtx.TextStyleId = db.Textstyle;// current text size
                mtx.TextHeight = db.Dimtxt;// current textstyle
                mtx.Width = 0.0;
                mtx.Contents = Clipboard.GetText();// paste mtext contents from clipboard
                // set alignment wharever you need:
                mtx.Attachment = AttachmentPoint.BottomCenter;
                mtx.SetAttachmentMovingLocation(mtx.Attachment);
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                btr.AppendEntity(mtx);
                tr.AddNewlyCreatedDBObject(mtx, true);
                mtx.Width = mtx.ActualWidth;
                tr.Commit();
            }
           // Clipboard.Clear();// optional, if you won't use it then
        }

   VB.NET

   
Code: [Select]
  <System.Security.SuppressUnmanagedCodeSecurity()> _
<DllImport("acad.exe", EntryPoint:="acedCmd", CharSet:=CharSet.Unicode, CallingConvention:=CallingConvention.Cdecl)> _
        Private Shared Function acedCmd(resbuf As IntPtr) As Integer
        End Function
        '-----------------------------------------------------------//
        ' AutoCAD 2010 (32bit eng.) Win7
        <CommandMethod("mtx")> _
        Public Sub createMtext()
            System.Windows.Clipboard.Clear()
            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument

            Dim ed As Editor = doc.Editor
            Dim db As Database = doc.Database
            Dim tr As Transaction = db.TransactionManager.StartTransaction()
            Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("Add then ""Cut"" text from Notepad")
            System.Threading.Thread.Sleep(500)

            Dim rb As New ResultBuffer()

            rb.Add(New TypedValue(CInt(LispDataType.Text), "_startapp"))
            rb.Add(New TypedValue(CInt(LispDataType.Text), "notepad"))
            rb.Add(New TypedValue(CInt(LispDataType.Text), ""))
            ' open file Unnamed.txt:
            rb.Add(New TypedValue(CInt(LispDataType.Text), vbLf))
            acedCmd(rb.UnmanagedObject)
            ed.Regen()
            Using tr
                Dim pt As Point3d = ed.GetPoint(vbLf & "Pick mtext location: ").Value
                Dim mtx As New MText()
                mtx.Location = pt
                mtx.SetDatabaseDefaults()
                mtx.TextStyleId = db.Textstyle
                ' current text size
                mtx.TextHeight = db.Dimtxt
                ' current textstyle
                mtx.Width = 0.0
                ' paste mtext contents from clipboard
                mtx.Contents = System.Windows.Clipboard.GetText()
                ' set alignment wharever you need:
                mtx.Attachment = AttachmentPoint.BottomCenter
                mtx.SetAttachmentMovingLocation(mtx.Attachment)
                Dim btr As BlockTableRecord = DirectCast(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
                btr.AppendEntity(mtx)
                tr.AddNewlyCreatedDBObject(mtx, True)
                mtx.Width = mtx.ActualWidth
                tr.Commit()
            End Using
            ' System.Windows.Clipboard.Clear();'' optional, if you won't use it then
        End Sub