Author Topic: Import text-value by picking in a windowsform  (Read 4257 times)

0 Members and 1 Guest are viewing this topic.

cadplayer

  • Bull Frog
  • Posts: 390
  • Autocad Civil3d, OpenDCL.Runtime, LISP, .NET (C#)
Import text-value by picking in a windowsform
« on: March 27, 2014, 06:19:38 AM »
Hello!

Can somebody provide me a example to do this:

-Pick a button in Wform
-select a textObject in Autocad drawing
-transport textvalue to a textLabel in the form

Locke

  • Guest
Re: Import text-value by picking in a windowsform
« Reply #1 on: March 27, 2014, 07:00:16 AM »
Does the form get spawned from inside AutoCAD? Or does it get instanciated from an external application?


cadplayer

  • Bull Frog
  • Posts: 390
  • Autocad Civil3d, OpenDCL.Runtime, LISP, .NET (C#)
Re: Import text-value by picking in a windowsform
« Reply #2 on: March 27, 2014, 07:46:00 AM »
Does the form get spawned from inside AutoCAD?
YES

code should run inside of Acad and display windowsForm first and then click a button to select a text. windowsForm should display again with the value of the text.

Locke

  • Guest
Re: Import text-value by picking in a windowsform
« Reply #3 on: March 27, 2014, 08:10:28 AM »
The logic will likely be something along the lines of:

- Selection prompt with MText/DbText filter
- Open selected text entity for read
- Set label value

What have you tried so far?

BillZndl

  • Guest
Re: Import text-value by picking in a windowsform
« Reply #4 on: March 27, 2014, 08:17:45 AM »
You'll need a button for click event and a label on your form:

Code: [Select]
private void button1_Click(object sender, EventArgs e)
        {
            PickTextInDrawing.SelectTextInDwg(label1);           
        } 

Code: [Select]
public static void SelectTextInDwg(Label label1)
        {
            Document document = AcadApp.DocumentManager.MdiActiveDocument;
            Editor editor = document.Editor;
            Database database = HostApplicationServices.WorkingDatabase;

            PromptEntityOptions options = new PromptEntityOptions("\nSelect Text Object: < pick > ");
            options.AllowNone = false;
            options.SetRejectMessage("\nMust be DBText. ");
            options.AddAllowedClass(typeof(DBText), false);
           
            Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView();
            PromptEntityResult selection = editor.GetEntity(options);

            if (selection.Status == PromptStatus.OK)
            {
                using (document.LockDocument())
                {
                    using (Transaction trans = database.TransactionManager.StartTransaction())
                    {                       

                      DBText text = trans.GetObject(selection.ObjectId, OpenMode.ForRead, false, false) as DBText;                     
                      label1.Text = text.TextString;
                      trans.Commit();
                    }
                }               
            }
            else
            {
                label1.Text = "Missed Pick:";
            }
        }

cadplayer

  • Bull Frog
  • Posts: 390
  • Autocad Civil3d, OpenDCL.Runtime, LISP, .NET (C#)
Re: Import text-value by picking in a windowsform
« Reply #5 on: March 27, 2014, 08:45:24 AM »
Sorry, please can you give me all usings directive I don't get it that forms and application talk together

BillZndl

  • Guest
Re: Import text-value by picking in a windowsform
« Reply #6 on: March 27, 2014, 08:57:52 AM »
Sorry, please can you give me all usings directive I don't get it that forms and application talk together

Here ya go.

Code: [Select]
using System.Windows.Forms;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace DisplayTextValue
{
    class PickTextInDrawing
    {
        public static void SelectTextInDwg(Label label1)
        {
            Document document = AcadApp.DocumentManager.MdiActiveDocument;
            Editor editor = document.Editor;
            Database database = HostApplicationServices.WorkingDatabase;

            PromptEntityOptions options = new PromptEntityOptions("\nSelect Text Object: < pick > ");
            options.AllowNone = false;
            options.SetRejectMessage("\nMust be DBText. ");
            options.AddAllowedClass(typeof(DBText), false);
           
            Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView();
            PromptEntityResult selection = editor.GetEntity(options);

            if (selection.Status == PromptStatus.OK)
            {
                using (document.LockDocument())
                {
                    using (Transaction trans = database.TransactionManager.StartTransaction())
                    {                       

                      DBText text = trans.GetObject(selection.ObjectId, OpenMode.ForRead, false, false) as DBText;                     
                      label1.Text = text.TextString;
                      trans.Commit();
                    }
                }               
            }
            else
            {
                label1.Text = "Missed Pick:";
            }
        }

    }
}

Form:
Code: [Select]
using System;
using System.Windows.Forms;

namespace DisplayTextValue
{
    public partial class TextValue : Form    {
       
        private static TextValue instance = null;
       
        public static TextValue Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new TextValue();
                }
                return instance;
            }
        }

        public TextValue()
        {
            InitializeComponent();
        }

        [STAThread]       
        static void Main(string[] args)
        {
           
          System.Windows.Forms.Application.Run(new TextValue());

        }               

        #region contains event handlers and methods.

        /// <summary>
        /// user buttonClick.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>       
       

        //exit button.
        private void button1_Click(object sender, EventArgs e)
        {
            base.Close();
        }
        //select text button.
        private void button2_Click(object sender, EventArgs e)
        {
            PickTextInDrawing.SelectTextInDwg(label2);           
        }             
       
        #endregion 
    }
}

BillZndl

  • Guest
Re: Import text-value by picking in a windowsform
« Reply #7 on: March 27, 2014, 09:00:30 AM »
Sorry,
forgot the command:
Code: [Select]
using Autodesk.AutoCAD.Runtime;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;


namespace DisplayTextValue
{
    public static class DisplayForm
    {
        /// <summary>
        /// command name to display dialog.
        /// </summary>
       
       
            [CommandMethod ("PkTxt", CommandFlags.Session)]
            public static void ShowModalDialog()
            {
                AcadApp.ShowModalDialog(TextValue.Instance);
            }

       
    }
}

Locke

  • Guest
Re: Import text-value by picking in a windowsform
« Reply #8 on: March 27, 2014, 09:07:26 AM »
Sorry, please can you give me all usings directive I don't get it that forms and application talk together

A Label is a class that lives under System.Windows.Forms and inherits from control.  The above approach sends the label as a parameter to the method that gets the selected text value.  An alternate method would be returning a string from a method like this:

Code: [Select]
public static string GetSelectedTextInDwg()
{
    // Do work...
    return textEnt.TextString;
}

This would make it more flexible later on if you wanted it to extend beyond the scope of a label control.  However as with both methods, using parameters is generally safer than publicly exposing the label and setting it from within a method,

BillZndl

  • Guest
Re: Import text-value by picking in a windowsform
« Reply #9 on: March 27, 2014, 09:26:51 AM »
Sorry, please can you give me all usings directive I don't get it that forms and application talk together

Locke is better explaining the technicalities than I am.
I just posted quick & dirty example as you requested.

Basically you are calling the 'SelectTextInDwg' method  from the form with the following click event.
The EditorGetEntity hides the modal form while the user picks.

Code: [Select]
private void button2_Click(object sender, EventArgs e)
        {
            PickTextInDrawing.SelectTextInDwg(label2);           
        }


I've done it both ways, calling a method that returns a string but the void method using a parameter is a cleaner way of doing things IMHO.


cadplayer

  • Bull Frog
  • Posts: 390
  • Autocad Civil3d, OpenDCL.Runtime, LISP, .NET (C#)
Re: Import text-value by picking in a windowsform
« Reply #10 on: March 27, 2014, 09:32:13 AM »
Hi!
Thank you so much!
It was a little hard way, to get understanding how communicate form.cs and program.cs. Also windowsForm and AcadApplication.
How does it look like here ?

BillZndl

  • Guest
Re: Import text-value by picking in a windowsform
« Reply #11 on: March 27, 2014, 09:49:16 AM »
You're welcome!
Glad to help.



Locke

  • Guest
Re: Import text-value by picking in a windowsform
« Reply #12 on: March 27, 2014, 09:52:07 AM »
I've done it both ways, calling a method that returns a string but the void method using a parameter is a cleaner way of doing things IMHO.

May I attempt to change your mind?

1.  The data we care about is DbText.TextString; System.String (reference value).  When we send a control to the method, it's carrying additional information (inherited from Control) that we don't care about.  While not a huge overhead, parameters generally should be used to bring information the method  needs to do its job.  In this case, it doesn't need anything to retrieve the text from a selected entity.

2.  Using return methods open up chaining possibilities for handy code:
Code - Text: [Select]
  1. var lblText.Text = GetSelectedText().ToUpper(); // Given the method is not written poorly
  2.  

3.  Returning a string makes the same routine usable in different scenarios without changing any code:
Code - Text: [Select]
  1. stringList.Add(GetSelectedText());
  2. MessageBox.Show(GetSelectedText());
  3.  

Locke is better explaining the technicalities than I am.

I still have a long way to go, but I am passionate about the language.


BillZndl

  • Guest
Re: Import text-value by picking in a windowsform
« Reply #13 on: March 27, 2014, 10:08:25 AM »
You make a good point Locke. :)
My problem is, I don't do this stuff often enough to really get good at it.
I'm more of a "Fireman" at my job than I am a programmer.
Putting out the fires (problems) that pop up around here in our CAD dept.

Thanks for your help and information.
I got a lot of help around here when I first started so I thought I'd try to help the OP.
I still have a long way to go myself!

I've done it both ways, calling a method that returns a string but the void method using a parameter is a cleaner way of doing things IMHO.

May I attempt to change your mind?

1.  The data we care about is DbText.TextString; System.String (reference value).  When we send a control to the method, it's carrying additional information (inherited from Control) that we don't care about.  While not a huge overhead, parameters generally should be used to bring information the method  needs to do its job.  In this case, it doesn't need anything to retrieve the text from a selected entity.

2.  Using return methods open up chaining possibilities for handy code:
Code - Text: [Select]
  1. var lblText.Text = GetSelectedText().ToUpper(); // Given the method is not written poorly
  2.  

3.  Returning a string makes the same routine usable in different scenarios without changing any code:
Code - Text: [Select]
  1. stringList.Add(GetSelectedText());
  2. MessageBox.Show(GetSelectedText());
  3.  

Locke is better explaining the technicalities than I am.

I still have a long way to go, but I am passionate about the language.

Locke

  • Guest
Re: Import text-value by picking in a windowsform
« Reply #14 on: March 27, 2014, 10:19:52 AM »
I got a lot of help around here when I first started so I thought I'd try to help the OP.

My nitpicking definitely doesn't undo your awesome help.  OP had a problem; you swooped in and put out the fire.  Think of me more like a technical adviser suggesting tweaks to improve nozzle efficiency.  That being said, this particular adviser is super open to correction if I'm way off base on something.