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

Textbox to Xdata

3 REPLIES 3
Reply
Message 1 of 4
Devin_11
269 Views, 3 Replies

Textbox to Xdata

I am working on a task where in I have to enter text in textbox of a wpf, and when I select a line and click a button the string entered in the textbox must be added as Xdata to that line. However when I do this, I am unable to see the entered string in Xdata list. Please see the code below:

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


namespace winformtest
{
    
    public class Lineselect
    {
      //  [CommandMethod("kfintest", CommandFlags.UsePickSet)]
        public static void LinCom()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            string appName = "testApp";
            ucWPF uc = new ucWPF(); //ucWPF is name of xaml file
            //string length = uc.lenbox.Text;
            string length = uc.lenbox.Text.ToString(); //lenbox is name of textbox here.s
            // string length = Convert.FromBase64String ( len);

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                PromptSelectionResult psr = ed.GetSelection();
              
                if (psr.Status == PromptStatus.OK)
                {
                    SelectionSet ss = psr.Value;
                    RegAppTable RAT;
                    RAT = tr.GetObject(db.RegAppTableId, OpenMode.ForRead) as RegAppTable;

                    if(RAT.Has(appName) ==  false)
                    {
                        using (RegAppTableRecord RATRec = new RegAppTableRecord())
                        {
                            RATRec.Name = appName;
                            tr.GetObject(db.RegAppTableId, OpenMode.ForWrite);
                            RAT.Add(RATRec);
                            tr.AddNewlyCreatedDBObject(RATRec,true);
                        }
                    }

                    using (ResultBuffer rb = new ResultBuffer())
                    {
                        rb.Add(new TypedValue((int)DxfCode.ExtendedDataRegAppName, appName));
                        rb.Add(new TypedValue((int)DxfCode.ExtendedDataAsciiString,length));

                        foreach(SelectedObject ssobj in ss)
                        {
                            Entity ent = tr.GetObject(ssobj.ObjectId,OpenMode.ForWrite) as Entity;
                            ent.XData = rb;
                        }
                    }



               // Formclass.Showpal();
                }
                tr.Commit();
            }
         //   Line2dCollection l2d = new Line2dCollection();
           // else tf.Visible = false; 
        }

    }
}

 

Is the string entered in  a textbox stored in a different data type other than string? If yes then how do I make this work? 

 

Thanks in advance.

Labels (3)
3 REPLIES 3
Message 2 of 4
norman.yuan
in reply to: Devin_11

It is not very clear, to me, what is you really ask. When you say "...I am unable to see the entered string in Xdata list...", do you mean you actually did a debugging run, examined the value in the variable "length" and saw its value is empty/null? Assume that is the case, then it is obviously the UI you created do not have a value assigned to the said text box.

 

Since you commented out the CommandMethod attribute, it is not clear how this code (the method LinCom()) runs or is called, from where. Also, if the ucWPF is only the UI class (as your comment indicates), then does the said text box in the WPF design (xaml file) has its "Text" property set to a value?

 

Shouldn't you need to show the UI to user so that user can enter a valid value into the said textbox, BEFORE you use the textbox' value for creating XData (or for whatever purpose)?

 

Other critiques:

1. It would be better to get user to get selections from Editor before starting a Transaction. That is, only start a Transaction when the selection result's status is PromptStatus.OK;

2. You do not create a single ResultBuffer as the XData and assign it to multiple selected entities. You should:

foreach (var selected in ss)

{

   ResultBuffer rb=.....

   ent.XData=rb;

}

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 4
Devin_11
in reply to: Devin_11

Hi, yes I did a debugging run but somehow the variable "length" shows as null. I believed I assigned a value, please see the code below:

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


namespace winformtest
{
    
    public class Lineselect
    {
          [CommandMethod("kfintest", CommandFlags.UsePickSet)]
       // static PaletteSet palette = Formclass.ps;
        public static void LinCom()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            string appName = "testApp";
         
            propclass pc = new propclass();
            
            string lentry = pc.lengthinput;
         
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                PromptSelectionResult psr = ed.GetSelection();
              
                if (psr.Status == PromptStatus.OK)
                {
                    SelectionSet ss = psr.Value;
                    RegAppTable RAT;
                    RAT = tr.GetObject(db.RegAppTableId, OpenMode.ForRead) as RegAppTable;

                    if(RAT.Has(appName) ==  false)
                    {
                        using (RegAppTableRecord RATRec = new RegAppTableRecord())
                        {
                            RATRec.Name = appName;
                            tr.GetObject(db.RegAppTableId, OpenMode.ForWrite);
                            RAT.Add(RATRec);
                            tr.AddNewlyCreatedDBObject(RATRec,true);
                        }
                    }

                    using (ResultBuffer rb = new ResultBuffer())
                    {
                        rb.Add(new TypedValue((int)DxfCode.ExtendedDataRegAppName, appName));
                        rb.Add(new TypedValue((int)DxfCode.ExtendedDataAsciiString,lentry));

                        foreach(SelectedObject ssobj in ss)
                        {
                            Entity ent = tr.GetObject(ssobj.ObjectId,OpenMode.ForWrite) as Entity;
                            ent.XData = rb;
                        }
                    }

                }
                tr.Commit();
            }
        
        }

    }
}
namespace winformtest
{
    public class propclass
    {
        private string lip;
        public string lengthinput { get => lip; set => lip=value; }
    }
}

amespace winformtest
{
    /// <summary>
    /// Interaction logic for ucWPF.xaml
    /// </summary>
    public partial class ucWPF : UserControl //, INotifyPropertyChanged
    {
        public ucWPF()
        {
           // DataContext = this;
            InitializeComponent();
            lenbox.Text = string.Empty;
            propclass propclass = new propclass()
            {
                
                lengthinput = lenbox.Text
              // lengthinput = "testing"
            };

            this.DataContext = propclass;

        }
}
namespace winformtest
{
    public class Formclass
    {
        public static PaletteSet ps = null;
        [CommandMethod("kpal" , CommandFlags.UsePickSet)]

        public static void Showpal()
        {

           // testform tf = new testform();
            //tf.Visible = true;
            if (ps == null)
            {
                ps = new PaletteSet("TestPalette");
                ps.Size = new System.Drawing.Size(200, 400);
                ps.DockEnabled = (DockSides) ((int)DockSides.Left + (int)DockSides.Right);
                ucWPF uc = new ucWPF();
                
                ElementHost eh = new ElementHost();
                ps.AddVisual("Pipe", uc);
             //   ps.Add("Pipe2", tf);
                eh.AutoSize = true;
                eh.Dock = DockStyle.Fill;
            }
            ps.KeepFocus = true;
            ps.Visible= true;
        }
    }
}


 

I have used a palette "ps", in which I have placed the WPF "ucWPF". But the string "lentry" (which was length in the previous code) doesn't seem to get updated. The WPF is visible to the user as soon as the command "KPAL" is entered. Here is how the UI looks:

Devin_11_0-1696613643182.png

I am trying to read the values from one of the textboxes. 

Message 4 of 4
Devin_11
in reply to: norman.yuan

Hi, yes I did a debugging run but somehow the variable "length" shows as null. I believed I assigned a value, please see the code below:

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


namespace winformtest
{
    
    public class Lineselect
    {
          [CommandMethod("kfintest", CommandFlags.UsePickSet)]
       // static PaletteSet palette = Formclass.ps;
        public static void LinCom()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            Database db = doc.Database;
            string appName = "testApp";
         
            propclass pc = new propclass();
            
            string lentry = pc.lengthinput;
         
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                PromptSelectionResult psr = ed.GetSelection();
              
                if (psr.Status == PromptStatus.OK)
                {
                    SelectionSet ss = psr.Value;
                    RegAppTable RAT;
                    RAT = tr.GetObject(db.RegAppTableId, OpenMode.ForRead) as RegAppTable;

                    if(RAT.Has(appName) ==  false)
                    {
                        using (RegAppTableRecord RATRec = new RegAppTableRecord())
                        {
                            RATRec.Name = appName;
                            tr.GetObject(db.RegAppTableId, OpenMode.ForWrite);
                            RAT.Add(RATRec);
                            tr.AddNewlyCreatedDBObject(RATRec,true);
                        }
                    }

                    using (ResultBuffer rb = new ResultBuffer())
                    {
                        rb.Add(new TypedValue((int)DxfCode.ExtendedDataRegAppName, appName));
                        rb.Add(new TypedValue((int)DxfCode.ExtendedDataAsciiString,lentry));

                        foreach(SelectedObject ssobj in ss)
                        {
                            Entity ent = tr.GetObject(ssobj.ObjectId,OpenMode.ForWrite) as Entity;
                            ent.XData = rb;
                        }
                    }

                }
                tr.Commit();
            }
        
        }

    }
}
namespace winformtest
{
    public class propclass
    {
        private string lip;
        public string lengthinput { get => lip; set => lip=value; }
    }
}

amespace winformtest
{
    /// <summary>
    /// Interaction logic for ucWPF.xaml
    /// </summary>
    public partial class ucWPF : UserControl //, INotifyPropertyChanged
    {
        public ucWPF()
        {
           // DataContext = this;
            InitializeComponent();
            lenbox.Text = string.Empty;
            propclass propclass = new propclass()
            {
                
                lengthinput = lenbox.Text
              // lengthinput = "testing"
            };

            this.DataContext = propclass;

        }
}
namespace winformtest
{
    public class Formclass
    {
        public static PaletteSet ps = null;
        [CommandMethod("kpal" , CommandFlags.UsePickSet)]

        public static void Showpal()
        {

           // testform tf = new testform();
            //tf.Visible = true;
            if (ps == null)
            {
                ps = new PaletteSet("TestPalette");
                ps.Size = new System.Drawing.Size(200, 400);
                ps.DockEnabled = (DockSides) ((int)DockSides.Left + (int)DockSides.Right);
                ucWPF uc = new ucWPF();
                
                ElementHost eh = new ElementHost();
                ps.AddVisual("Pipe", uc);
             //   ps.Add("Pipe2", tf);
                eh.AutoSize = true;
                eh.Dock = DockStyle.Fill;
            }
            ps.KeepFocus = true;
            ps.Visible= true;
        }
    }
}


 

I have used a palette "ps", in which I have placed the WPF "ucWPF". But the string "lentry" (which was length in the previous code) doesn't seem to get updated. The WPF is visible to the user as soon as the command "KPAL" is entered. Here is how the UI looks:

Devin_11_0-1696613790604.png

 

I am trying to read the values from one of the textboxes. 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report