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

User Interface

4 REPLIES 4
Reply
Message 1 of 5
Anonymous
415 Views, 4 Replies

User Interface

Chaps,

 

My sole use for VBA was to provide a very pretty dialogue box in which to enter information which is then passed to a LISP routine via a text file.

 

This has worked fine for many versions of AutoCAD, but since upgrading to 2015 from 2014 (on the same 64-bit laptop) this hasn't worked. Looks like I would need to re-write part or all of my programs so I've decided to bin VBA and switch to, arbitrarily, C#.

 

A very big ask, but could some kind soul start me off? I'd like a "UserForm" with two "TextBoxes". The first box is filled with the first line of D:\Test-In.txt and the contents of the second is written to D:\Test-Out.txt when the [OK] button is pressed.

 

Is this possible?

 

S

4 REPLIES 4
Message 2 of 5
Anonymous
in reply to: Anonymous

Definitely possible.
Have you done any C# programming? If not go to http://www.visualstudio.com/en-us/products/visual-studio-express-vs.aspx and download the Express Edition for Windows Desktop. You will need the ObjectArx libraries, Google it and you will find it.

Start with running through some tutorials on Windows Form Applications. Since what you want is such a simple form, some of the basic tutorials will work. Learn a bit about C# first, then move onto a AutoCAD addon.

Go here: http://msdn.microsoft.com/en-us/vstudio/dn439939, maybe you should start on this page. It will help you get started with Visual Studio.

Regards,
Message 3 of 5
Anonymous
in reply to: Anonymous

Brian,

Apologies for the delay. These look like useful links - thanks for the assistance.

S
Message 4 of 5
Dale.Bartlett
in reply to: Anonymous

C# is definitely the preferred long term skill, however you may consider a "quick fix" and get into the .NET family via VB.NET. I don't have the link to hand, but google:

AutoCAD VBA to VB.NET Migration Basics by Augusto Goncalves ADN

I think it is an ADN DevTV video and material.

Below is sample to write a text file in C#:

Dale

<code>

 

// from MSDN sample 

// DJB overloaded version - for single text string 

public void WriteTextFile(string pstrFileNameFull, bool blnAppend, string pstrText)

if (blnAppend == true)

// Example #4: Append new text to an existing file 

using (System.IO.StreamWriter file = new System.IO.StreamWriter(pstrFileNameFull, true))

{

file.WriteLine(pstrText);

}

else

// Example #2: Write one string to a text file.

System.IO.

File.WriteAllText(pstrFileNameFull, pstrText);

}

}

<code>

 




______________
Yes, I'm Satoshi.
Message 5 of 5
bjhuffine
in reply to: Anonymous

Since you're starting off, definitely read up on Windows Forms applications using MSDN or other sources.  I'm not where I can look, but maybe someone will throw out a few references.  I'm making a small assumption that you know how to set this up, but just in case here's a brief set of steps.

 

  1. Start project as class library.  Add references from ObjectARX 2015 inc directory to accoremgd.dll, acmgd.dll, and acdbmgd.dll (all copy local properties set to false).
  2. On project properties, go to the Debug tab, set "Start external program" to C:\Program Files\Autodesk\AutoCAD 2015\acad.exe and set "Working directory" to C:\Program Files\Autodesk\AutoCAD 2015
  3. right-click on project, Add new item ... Windows Form
  4. Add textboxes, buttons, etc through designer.

Jerry Winters has a nice book too on programming with AutoCAD that's really good.  It uses VB, but that can easily be translated to C#. (http://www.vbcad.com/)

 

Since the application will be started via an AutoCAD command, here's the command code:

 

[assembly:CommandClass(typeof(Commands))]

namespace AutoCADApp2
{
    public class Commands
    {
        [CommandMethod("RunWindowsApp")]
        public void RunWindowsApp()
        {
            MyUserForm form = new MyUserForm();
            form.Show();

        }
    }
}

 Here's the form code behind... it's super generic using poor programming practices, but hopefully will help you get kickstarted in the right direction.

 

 public partial class MyUserForm : Form
    {
        public MyUserForm()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            //poor coding practices but should hopefully get the point across...
            //needs processes separated, better exception handling, etc...

            //Read Text-In and update textbox
            FileInfo fileTextIn = new FileInfo(@"C:\acad\Text-In.txt");

            if (!fileTextIn.Exists)
                MessageBox.Show("File Text-In.txt does not exist.");

            using (StreamReader reader = fileTextIn.OpenText())
            {
                //read first line
                string line = reader.ReadLine();

                //if no data in the file it will return null... therefore
                if (line != null)
                    textBox1.Text = line;
            }

            //Now write to the Text-Out.txt
            FileInfo fileTextOut = new FileInfo(@"c:\acad\Text-Out.txt");

            //Create if not already there...
            //There should be more elegant ways to handle this, but hopefully this is straight forward
            if (!fileTextOut.Exists)
            {
                fileTextOut.Create();
                fileTextOut.Refresh();  //since the file was just created, refresh state of FileInfo object
            }

            using (StreamWriter writer = fileTextOut.AppendText())
            {
                if (!string.IsNullOrEmpty(textBox2.Text))
                    writer.WriteLine(textBox2.Text);
            }
        }
    }

 

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost