Results 1 to 7 of 7

Thread: AddIn Programming guidance?

  1. #1
    Member TRWhitehead's Avatar
    Join Date
    February 16, 2012
    Location
    Portland, OR
    Posts
    332
    Current Local Time
    07:02 AM

    Not allowed! Not allowed!

    AddIn Programming guidance?

    Hi All,

    It has been a while but I'm diving into developing some plug-ins/tools for our company. Pretty specific stuff to our workflow but needing some guidance.

    I want to launch a window to gather input from the user, then use that input to create sheets. I can get the form to display, it seems to work, but the inputs don't do anything. I've seen various posts around the internet about how to use forms with a normal application but nothing related to API/Revit specifically. Or my google-fu is out of practice and I can't find it.

    Here's the question:
    1) How do I gather input from the form and then set variables in my main program?

    What I've tried:
    Setting the variables as global but I can't seem to get them to update in the form.
    when the form closes, set the sheet variables = the variables from the form.

    I'm about to dive into using WPF instead of forms in hopes that would be more documented.

    THanks and I hope you are staying safe,
    Tom

  2. #2
    Member
    Join Date
    March 16, 2012
    Posts
    50
    Current Local Time
    09:02 AM

    Not allowed! Not allowed!
    I'm not going to tell you not to learn WPF, but, in my experience, WinForms variables are way easier than WPF variables.


    It sounds like you want to open a form to collect the desired setting(s) from the user, save the setting(s) and close the form, and then use a separate trigger to run your "main program."

    Try this:


    Follow the diagram to bind a setting to your form's control, in this case, a text box. Let's say you called it "ImportantSheetVariable".


    In the FormClosing event (or the "OK" button event) of your form, add:


    Properties.Settings.Default.Save();


    Now you can access the setting from your main program like so:


    string importantsheetvariable = Properties.Settings.Default.ImportantSheetVariable ;


    This uses the built-in Windows settings process and requires no additional setup from you.



    Click image for larger version. 

Name:	Bind Setting To Form Control.png 
Views:	26 
Size:	63.9 KB 
ID:	38612

  3. #3
    Member
    Join Date
    March 16, 2012
    Posts
    50
    Current Local Time
    09:02 AM

    Not allowed! Not allowed!
    Sorry. You will also need to create a default settings file, if you don't already have one.

    Under Properties > Settings, click "This project does not contain a default settings file. Click here to create one."

    Click image for larger version. 

Name:	Create a default setting file.png 
Views:	18 
Size:	21.7 KB 
ID:	38614
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	Create a default setting file.png 
Views:	14 
Size:	16.9 KB 
ID:	38613  

  4. #4
    Member TRWhitehead's Avatar
    Join Date
    February 16, 2012
    Location
    Portland, OR
    Posts
    332
    Current Local Time
    07:02 AM

    Not allowed! Not allowed!
    Thanks Peter,

    I'll look into that. I'm also looking at some examples from Jeremy Tammik but they don't seem to quite do what i want. I feel like I'm missing something really basic as I figure this should be fairly common and simple.

  5. #5
    Member kraftwerk's Avatar
    Join Date
    November 1, 2012
    Location
    St. Louis, MO
    Posts
    71
    Current Local Time
    09:02 AM

    1 Not allowed! Not allowed!
    In XAML, you could say something like get some values from something you have just completed on the Window or UserControl that you have just completed and then take this and run another method from there to complete some other action. Let's see if this pastes correctly:

    Code:
    private void CloseButton_Click(object sender, RoutedEventArgs e)        {
                Workset k = (Workset)WorksetComboSelector.SelectedItem;
                if(k != null)
                {
                    WorksetId worksetid = Worksets.WorksetUtilities.WorksetIdByName(document, k.Name, false);
                    Worksets.WorksetUtilities.SetActiveWorkset(document, worksetid);
                }
                Worksets.WorksetController.window.Hide();
            }
    I'm running two different methods here and waiting for the user to hit ok before gathering that information and doing something with it. Then hiding that dialog box.

    XAML is nice because you drag and drop a button on to the user control, double-click and it will produce the event handler for you.

    Now, let's say that this is overkill and you would like to stay with WinForms. Cool, if it ain't broke, right? Bind again to another button and call out to another method to complete some task.

    Code:
    private void btnOK_Click(object sender, EventArgs e)
            {
                //If the textbox the user was supposed to fill is not empty let's doing something with that string of text.
                if (!string.IsNullOrWhiteSpace(txtPlaneName.Text))
                {
                    App.ReferencePlane.Rename(App.Doc, txtPlaneName.Text);
                    //For Tom's example we could create a static string variable in the App context and set it here
                    App.tomsvariablename = txtPlaneName.Text;
                    DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("Name cannot be empty.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }

  6. #6
    Member TRWhitehead's Avatar
    Join Date
    February 16, 2012
    Location
    Portland, OR
    Posts
    332
    Current Local Time
    07:02 AM

    Not allowed! Not allowed!
    Thanks kraftwerk,

    I'm not as comfortable with the XAML but I'm going to try it shortly.



    On the second one. I think I understand what you're doing there. But let me put it into my own words (Think of it as rubber duck debugging):

    In the .cs for the form, you're adding an event to btnOK. First you check to make sure that txtPlaneName.Text (a text box in the window) isn't blank. That's the beginning if statement.

    The next line is telling App to rename a reference plane based on a textbox called txtPlaneName

    The next is saying get set the public, static variable "tomsvariablename" to the contents of txtPlaneName as text.

    My question is, does then the "App" take over and utilize those now set variables?

    I'm slowing groking how to reference the data in the form, just not how to hand that off back to the application/plugin to finish the job. Part of the issue is eventually I'll need to set more variables based on the input something like an array (List?) and I guess that should happen in the main app.

    Again, thanks... This is actually helpful, I'm just slow on the uptake. My programming experience is mostly hacking Arduinos.

  7. #7
    Member kraftwerk's Avatar
    Join Date
    November 1, 2012
    Location
    St. Louis, MO
    Posts
    71
    Current Local Time
    09:02 AM

    Not allowed! Not allowed!
    Quote Originally Posted by TRWhitehead View Post
    My question is, does then the "App" take over and utilize those now set variables?
    Yep. Since the application owns the context, as long as Revit is up and running, those variables contain the information we just set over from the WinForm. We could then kick off another method that reads the variable name that is in the Revit context and do something with it.

    So, hypothetically, here is some text to take that variable and set it to the project name and number.

    Code:
     using (Transaction projectInformationTransaction = new Transaction(doc, "Set Project Information"))
    {
                        projectInformationTransaction.Start();
                        doc.ProjectInformation.Name = App.tomsvariablename;
                        doc.ProjectInformation.Number = App.tomsOtherVariableName;
                        projectInformationTransaction.Commit();
    }
    The process to remember is that there has to be a chain of events that gets tracked. If a user clicks a button on a ribbon, then from that moment, you have to track when they click the button, when the dialog closes, what happens with the variables when it is closed, starting the transaction with Revit, setting/changing some information within Revit, then closing the transaction so the user can go back to work.

    So one of your buttons could look like this. This is a xaml example. Again, hypothetical. But similar in concept to WinForms.

    Code:
    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class ExecuteSettings : IExternalCommand
    {
        public static WindowSettings k;
        public static System.Windows.Window window = new System.Windows.Window();
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            try
            {
                    var l = window;
                    k = new WindowSettings();
                    l.Content = k;
                    l.WindowStyle = Window.WindowStyle;
                    l.ResizeMode = Window.ResizeMode;
                    l.Height = Window.Height;
                    l.Width = Window.Width;
                    l.ShowDialog();
                    ChangeWhateverTomWants(string App.tomsvariablename);
                    return Result.Succeeded;
            }
            catch
            {
                    return Result.Failed;
            }
       }
    }
    
    private static void ChangeWhateverTomWants(string stringname)
    {
          //change things here! 'Merica
    }

Similar Threads

  1. Need your guidance.
    By Redtape in forum MEP - Family Creation
    Replies: 2
    Last Post: August 29th, 2016, 10:40 AM
  2. Replies: 0
    Last Post: August 29th, 2013, 12:15 AM
  3. Guidance on First Project
    By domsib in forum MEP - General
    Replies: 1
    Last Post: November 21st, 2012, 12:17 PM
  4. Guidance on Families
    By domsib in forum MEP - Family Creation
    Replies: 13
    Last Post: November 12th, 2012, 01:55 PM
  5. Buildz: Programming Programming
    By Buildz in forum Blog Feeds
    Replies: 0
    Last Post: April 24th, 2012, 06:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •