1

I am trying to pull a variable out of a Windows Forms page created in one application and pass it into an AutoCAD C# .Net function I have written.

I am able to assign the value in the TextBox blockNameInput to another variable. I need to get that same information into AutoCAD so that I can then load all blocks with that name.

I have a class with properties for the blockName that I am setting and getting the value for. It all works in the same namespace. When I call the same variable in a different namespace, I get the default value "Old" instead of the value that was input into the TextBox.

I may be starting a new instance of BlockNameClass in the second namespace, but I have no idea what the solution could be. The relevant code is below:

//THIS IS THE OUT OF PROCESS AUTOCAD SCRIPT
namespace AttributeSyncExternal
{
    public partial class AttributeSyncForm : Form, IMessageFilter
    {
        public class BlockNameClass
        {
            public string blockName = "Old";
            public string BlockName
            {
                get
                {
                    return blockName;
                }
                set
                {
                    blockName = value;
                }
            }
        }

        public static readonly BlockNameClass _class = new BlockNameClass();
        public static BlockNameClass BlockNameClassInstance
        {
            get { return _class; } 
        }

         private void runButton_Click(object sender, EventArgs e)
        {
            BlockNameClassInstance.BlockName = blockNameInput.Text;
            //DO SOME OTHER STUFF HERE
        }
    }
}


using static AttributeSyncExternal.AttributeSyncForm;
//THIS IS THE IN PROCESS AUTOCAD SCRIPT
namespace AttributeSyncExternal.Attributesync
{
    public class DumpAttributes
    {
        [CommandMethod("LISTATT")]
        public void ListAttributes()
        {
            //DO SOME OTHER STUFF HERE
            string blockName = BlockNameClassInstance.BlockName;
        }
    }
}
  • I have edited your question. You mention "blocks" and "blocknames" without defining them, and these may not be relevant to the problem you are having. If you feel they are relevant, then you should briefly explain what they are so that those who are not AutoCAD experts can also help solve your problem. – Sabuncu Feb 07 '17 at 20:53
  • Thanks for the input, what a block is should not be important for answering the question. The issue broken down is simply that I cannot create an instance of a class in one namespace, assign a value to a variable, and then call that value in another namespace without it resetting that value back to default. – Tayloe Compton Feb 08 '17 at 01:14
  • changing BlockName to static caused errors and I was not able to compile. Changing public string blockName = "Old"; to public static string blockName = "old"; made no difference. – Tayloe Compton Feb 08 '17 at 02:38

3 Answers3

0

Try this.

// add this line:
using AttributeSyncExternal;

here: 

//THIS IS THE IN PROCESS AUTOCAD SCRIPT
namespace AttributeSyncExternal.Attributesync

edit:

Mr Tayloe my eyes hurt looking at the above code. TBH I couldn't understand it.

enter image description here

The below works for me. And it's a simple console application which illustrates what should happen:

namespace firstnamespacetest
{
    class Program
    {
        static void Main(string[] args)
        {
            test t = new test();

            Console.WriteLine(t.BlockName);
            // prints "Old value"

            // event handler changes value
            t.TextBoxHandler();

            //prints new value
            Console.WriteLine(t.BlockName);

            Console.ReadLine();
        }
    }
}

namespace secondNameSpace
{
    class test
    {
        private string _blockName;

        public string BlockName
        {
            get { return _blockName; }
            set { _blockName = value; }
        }

        public test()
        {
            // set initial value of blockname
            _blockName = "Old value";
        }

        public void TextBoxHandler()
        {
            _blockName = "New value of block";
        }
    }
}

the final result:

enter image description here

BenKoshy
  • 33,477
  • 14
  • 111
  • 80
  • I didn't include all of my usings in my code for simplification, but I am already using AttributeSyncExternal.AttributeSyncForm in my in process script. I edited my code to include this. – Tayloe Compton Feb 08 '17 at 03:11
  • ok. I"ll come back to this later tonight.....mr Tayloe ur code is very confusing, i'll provide a simplified example and you can have a look at that – BenKoshy Feb 08 '17 at 04:13
  • That would be much appreciated! – Tayloe Compton Feb 08 '17 at 04:17
  • ok mr Tayloe update with simple code sample which works on my end. perhaps replicate to suit your purposes? – BenKoshy Feb 08 '17 at 12:35
  • BKSpurgeon, thank you so much for your assistance in this matter. This would work if it were not for the fact that class test is a windows form page, and the information I am using to populate_blockName in TextBoxHandler() is coming from a user input on the form page. When I create a new instance of the form in the first namespace, it is making a new form page, and therefore does not have the user input. Therefore, when I run the code the way you have set it up, it returns a blank string. – Tayloe Compton Feb 08 '17 at 13:19
  • @TayloeCompton then a cheap hack solution is to store the variable in a persistent form - the registry perhaps. it aint pretty but it gets the job done fast. – BenKoshy Feb 08 '17 at 21:48
0

To use using static AttributeSyncExternal.AttributeSyncForm, the AttributeSyncForm class must be static

Maxence
  • 12,868
  • 5
  • 57
  • 69
0

On the AutoCAD side, the DumpAttribute class have to 'know' the running instance of AttribuSyncForm. Typically, a new instance of the form is created in an AutoCAD command and shown using Application.ShowModalDialog() (or ShowModelessDialog()). Doing so the CommandMethod can simply access to the AttribuSyncForm public instance properties.

On the Form side:

namespace AttributeSyncExternal
{
    public partial class AttributeSyncForm : Form
    {
        public AttributeSyncForm()
        {
            InitializeComponent();
        }

        public string BlockName
        {
            get { return blockNameInput.Text; }
            set { blockNameInput.Text = value; }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }
    }
}

On AutoCAD side

using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace AttributeSyncExternal.AttributeSync
{
    public class DumpAttributes
    {
        [CommandMethod("LISTATT")]
        public void ListAttributes()
        {
            using (var dlg = new AttributeSyncForm())
            {
                dlg.BlockName = "Old";
                if (AcAp.ShowModalDialog(dlg) == DialogResult.OK)
                {
                    AcAp.ShowAlertDialog(dlg.BlockName);
                }
            }
        }
    }
}
gileCAD
  • 2,295
  • 1
  • 10
  • 10