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;
}
}
}
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);
}
}
}
}
}