Texts to listbox C#

k005
Advisor
Advisor

Texts to listbox C#

k005
Advisor
Advisor

How can I send text objects in the dwg file to the listbox by selecting them?

 

common feature in texts to be selected = ø

 

sample text:

 

51ø16 / 20

 

 

Thanks

0 Likes
Reply
850 Views
13 Replies
Replies (13)

essam-salah
Collaborator
Collaborator

hi

do use wpf or winforms?

k005
Advisor
Advisor

Winforms    ( .dll )

0 Likes

essam-salah
Collaborator
Collaborator

hi @k005 

just create .NetFramwork ClassLibrary and add System.Windows.Forms assembly to your project references,

and try this:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using aDB = Autodesk.AutoCAD.DatabaseServices;
using aApp = Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using aGeom = Autodesk.AutoCAD.Geometry;

namespace ACADAPITutorial
{
    public class TextToListBox
    {
        #region current doc
        static aApp.Document aDoc => aApp.Application.DocumentManager.MdiActiveDocument;
        #endregion

        #region initialize
        private static bool isDialogOpen = false;
        private static SelectionDialog dialog;

        static TextToListBox()
        {
            dialog = new SelectionDialog();
            dialog.SelectBtm.Click += SelectBtm_Click;
            dialog.FormClosing += Dialog_FormClosing;
        }
        static void Dialog_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
            e.Cancel = true;
            dialog.Hide();
            isDialogOpen = false;
        }
        #endregion

        // select text
        private static void SelectBtm_Click(object sender, EventArgs e)
        {
            // define filter
            // TypedValue /filter criteria
            var typeValueArray = new aDB.TypedValue[]
            {
                    new aDB.TypedValue((int)aDB.DxfCode.Operator, "<or"), // start ORing
                    new aDB.TypedValue((int)aDB.DxfCode.Start, "Text"),
                    new aDB.TypedValue((int)aDB.DxfCode.Start, "MText"),
                    new aDB.TypedValue((int)aDB.DxfCode.Operator, "or>"), // end ORing
                    // to get any ACAD Obj dxf name (Text, MText , ...) use ACAD LIST command.
            }; 
            var filter = new SelectionFilter(typeValueArray);
            // selection options
            var selectionOptions = new PromptSelectionOptions()
            {
                MessageForAdding = "select texts",
            };
            // Request for objects to be selected in the drawing area
            var selectionResult = aDoc.Editor.GetSelection(selectionOptions, filter);
            // If the prompt status is OK, objects were selected
            if (selectionResult.Status != PromptStatus.OK)
            {
                aDoc.Editor.WriteMessage($"\n----- No selection found ------");
                return;
            }
            // get object IDs
            var allTextIDs = selectionResult.Value.GetObjectIds();
            List<string> allText = new List<string>();
            using (var ts = aDB.HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
            {
                Type type;
                foreach (var id in allTextIDs)
                {
                    var dbObj = id.GetObject(aDB.OpenMode.ForRead);
                    type = dbObj.GetType();
                    if (type.Equals(typeof(aDB.DBText)))
                    {
                        allText.Add(((aDB.DBText)dbObj).TextString);
                    }
                    else if (type.Equals(typeof(aDB.MText)))
                    {
                        allText.Add(((aDB.MText)dbObj).Text);
                    }
                }
            }
            dialog.ListBox.DataSource = allText;
        }

        [CommandMethod("cieSelectText")]
        public void SelectText()
        {
            // check already opened
            if (isDialogOpen) return;
            // show modeless dialog
            aApp.Application.ShowModelessDialog(dialog);
            isDialogOpen = true;
        }
    }
}

 

and here is a simple form:

 

public class SelectionDialog : System.Windows.Forms.Form
    {
        public System.Windows.Forms.ListBox ListBox { get; private set; }
        public System.Windows.Forms.Button SelectBtm { get; private set; }

        public SelectionDialog()
        {
            SelectBtm = new System.Windows.Forms.Button()
            {
                Text = "select...",
            };
            ListBox = new System.Windows.Forms.ListBox()
            { 
            
            };
            // wrap all controls into a panel
            var panel = new System.Windows.Forms.FlowLayoutPanel()
            {
                AutoScroll = true,
                FlowDirection = System.Windows.Forms.FlowDirection.TopDown,
                WrapContents = false,
            };
            panel.Controls.Add(SelectBtm);
            panel.Controls.Add(ListBox);
            // add controls to dialog
            this.Controls.Add(panel);
        }
    }

 

 

k005
Advisor
Advisor

I received the articles with the code you selected (text object) and sent to the listbox. I took one more step thanks to you. If I can complete it, it will be a good work.

Thank you so much..

 

yazılar.png

0 Likes

k005
Advisor
Advisor

I could not integrate the codes you sent to the project I was working on. I'm new to C #. I prepared a screen video to detail the subject.

 

https://www.dosya.tc/server33/j393xz/AcadPlugin.rar.html 


The choice will be based on the Layer and Text text. Mtext is not included in the selection.

Some texts (text objects) are composed of 2 texts, while some texts are 3 parts. I do not know how we can edit this. It takes a very long time.


For now, just: If we can take 3 pieces or 2 pieces of text like 1 51ø16 / 200 L = 10500 and put it into the listbox, it is enough for now.


Thank you very much for your help and Codes.

0 Likes

essam-salah
Collaborator
Collaborator

hi @k005 

couldn't open the .zip file you attached i think it's better if you make it a GIF so we can see it or a link to YouTube video,

if you want to select Text in a specific layer just change the above code typeValueArray to:

 

 var myLayerName = "myLayer";
            var typeValueArray = new aDB.TypedValue[]
            {
                    new aDB.TypedValue((int)aDB.DxfCode.Operator, "<and"),
                    new aDB.TypedValue((int)aDB.DxfCode.Start, "Text"),
                    new aDB.TypedValue((int)aDB.DxfCode.LayerName, myLayerName),
                    new aDB.TypedValue((int)aDB.DxfCode.Operator, "and>"), 
            };

 

and the question about a Text with text parts isn't clear actually,

k005
Advisor
Advisor

our road map has been determined. We must proceed from this part...

 

SelectBtm_Click

 

 

0 Likes

essam-salah
Collaborator
Collaborator

.

0 Likes

k005
Advisor
Advisor
0 Likes

mttlp
Contributor
Contributor

Merhaba videoları nı sesli çekeydin iyi olurdu donatı metraj programına yardımcı olurum. 

0 Likes

essam-salah
Collaborator
Collaborator

hi @k005 , i got your video

man it's a big task i don't think someone here will give you a complete project,

what i understand is you have some texts in your drawing and each 3(or 2) texts are related to each other which gives information about a rebar and your task is to collect these related texts together .

since you are not expert i suggest to look for someone in the industry.

k005
Advisor
Advisor

Sesli de hazırlarım. Ancak ücretli ile tam istediğim sonucu alamayabiliriz.

 

Öğrenmem gerekiyor.

 

 

Teşekkürler.

0 Likes

k005
Advisor
Advisor

@essam-salah 

 

Yes . the subject is exactly as you said. I am not an expert. but I understood the code structure. I need to get a listbox with Text objects filtered. I have included some of the code you sent in my Project.

Now the filtering is left.

 

Thank you so much.

0 Likes