TaskDialog user input

TaskDialog user input

dwightduronidavy
Enthusiast Enthusiast
1,516 Views
14 Replies
Message 1 of 15

TaskDialog user input

dwightduronidavy
Enthusiast
Enthusiast

Dear all,

The following application macro (for Revit 2024) does not allow the user to input using the TaskDialog function. 
I would appreciate a resolution concerning this matter.



using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;

namespace DDA_CSharpModule4
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.DB.Macros.AddInId("8469C11C-9426-4B76-8910-02E720C07230")]
    public partial class ThisApplication
    {
        private void Module_Startup(object sender, EventArgs e)
        {

        }

        private void Module_Shutdown(object sender, EventArgs e)
        {

        }

        #region Revit Macros generated code
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(Module_Startup);
            this.Shutdown += new System.EventHandler(Module_Shutdown);
        }
        #endregion

        public void RotateModelFileAboutIO()
        {
            UIDocument uidoc = this.ActiveUIDocument;
            Document doc = uidoc.Document;

            // Prompt the user for the rotation angle in degrees using a TaskDialog
            TaskDialog angleDialog = new TaskDialog("Rotate Model File About Internal Origin");
            angleDialog.MainInstruction = "Enter the angle (in degrees) to rotate the model:";
            angleDialog.MainContent = "Please enter a numeric value for the angle.";
            angleDialog.CommonButtons = TaskDialogCommonButtons.Ok | TaskDialogCommonButtons.Cancel;
            angleDialog.DefaultButton = TaskDialogResult.Ok;

            TaskDialogResult result = angleDialog.Show();

            if (result == TaskDialogResult.Cancel)
            {
                TaskDialog.Show("Info""Operation cancelled by user.");
                return;
            }

            // Here, we assume the user can input the value using a better method or predefined angles
            double rotationAngleDegrees = 0;
            // Alternatively, a list of predefined angles can be added for selection.

            // Convert the rotation angle to radians
            double rotationAngle = rotationAngleDegrees * (Math.PI / 180.0);

            // Get the internal origin point as the center of rotation
            XYZ centerPoint = XYZ.Zero;

            // Define the rotation axis (Z-axis)
            Line rotationAxis = Line.CreateBound(centerPoint, centerPoint + XYZ.BasisZ);

            // Collect all elements that can be transformed
            FilteredElementCollector collector = new FilteredElementCollector(doc)
                .WhereElementIsNotElementType();

            List<ElementId> elementIds = new List<ElementId>();

            foreach (Element element in collector)
            {
                // Skip certain elements that are not transformable (e.g., levels, views, phases, grids)
                if (!(element is Level) && !(element is View) && !(element is Phase) && !(element is Grid))
                {
                    elementIds.Add(element.Id);
                }
            }

            // Start a transaction to rotate the model
            using (Transaction transaction = new Transaction(doc, "Rotate Entire Model"))
            {
                try
                {
                    transaction.Start();

                    // Rotate all collected elements around the internal origin
                    ElementTransformUtils.RotateElements(doc, elementIds, rotationAxis, rotationAngle);

                    transaction.Commit();
                    TaskDialog.Show("Success"string.Format("The model has been successfully rotated by {0} degrees about the internal origin.", rotationAngleDegrees));
                }
                catch (Exception e)
                {
                    transaction.RollBack();
                    TaskDialog.Show("Error"string.Format("An error occurred while rotating the model: {0}", e.Message));
                }
            }
        }
    }
}

0 Likes
Accepted solutions (1)
1,517 Views
14 Replies
Replies (14)
Message 2 of 15

scgq425
Advocate
Advocate

Hi @dwightduronidavy :

Taskdialog just a prompt text window , if you want to get user input paramater value , can use the ` Microsoft.VisualBasic.Interaction.InputBox` API top make this style like the picture.

this dll reference import from `Microsoft.VisualBasic.dll`

and i edit you code , also have some problem.

1. you function not use in startUp

2. the activeDocuemnt patameter can get quicky.

 

```

[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.DB.Macros.AddInId("7F17BF95-744C-4A32-9E1E-79457755021B")]
    public partial class ThisDocument
    {
        private void Module_Startup(object sender, EventArgs e)
        {
            
            RotateModelFileAboutIO();
        }

        private void Module_Shutdown(object sender, EventArgs e)
        {

        }

        #region Revit Macros generated code
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(Module_Startup);
            this.Shutdown += new System.EventHandler(Module_Shutdown);
        }
        #endregion
        
    public void RotateModelFileAboutIO()
        {
            UIDocument uidoc =  Application.ActiveUIDocument;
            Document doc = uidoc.Document;

            string degreeStr = Microsoft.VisualBasic.Interaction.InputBox("input value" , "Rotate","0");
            TaskDialog.Show("Revit" , degreeStr);
            
            // This is Code 
        }
    }

```

LanHui Xu 徐兰辉
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

Blog
LinkedIn
Revit/CAD Development | Steel Product Manager

EESignature

0 Likes
Message 3 of 15

scgq425
Advocate
Advocate

scgq425_0-1733117965927.png

 

LanHui Xu 徐兰辉
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

Blog
LinkedIn
Revit/CAD Development | Steel Product Manager

EESignature

Message 4 of 15

dwightduronidavy
Enthusiast
Enthusiast

Thank you so very much!
Would it be at all possible for you to paste the entire macro code here?
I am fairly new to RevitAPI..

0 Likes
Message 5 of 15

scgq425
Advocate
Advocate

Hi @dwightduronidavy :

 i just add tow row target to create window to get the user input .

do you want to check or debug you code to done this function? 

1. the input window about :

```

string degreeStr = Microsoft.VisualBasic.Interaction.InputBox("input value" , "Rotate","0");

```

2.  Get ActiveDocument

```

  UIDocument uidoc =  Application.ActiveUIDocument;

```

in Revit macro this is vaild . 

just use Appliication can direction get .

LanHui Xu 徐兰辉
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

Blog
LinkedIn
Revit/CAD Development | Steel Product Manager

EESignature

0 Likes
Message 6 of 15

dwightduronidavy
Enthusiast
Enthusiast

This is my attempt (but with errors [attached])

using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualBasic; // Importing Microsoft.VisualBasic for InputBox usage

namespace DDA_CSharpModule4
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.DB.Macros.AddInId("8469C11C-9426-4B76-8910-02E720C07230")]
public partial class ThisApplication
{
private void Module_Startup(object sender, EventArgs e)
{
RotateModelFileAboutIO(); // Call RotateModelFileAboutIO on startup
}

private void Module_Shutdown(object sender, EventArgs e)
{
}

#region Revit Macros generated code
private void InternalStartup()
{
this.Startup += new System.EventHandler(Module_Startup);
this.Shutdown += new System.EventHandler(Module_Shutdown);
}
#endregion

public void RotateModelFileAboutIO()
{
UIDocument uidoc = this.Application.ActiveUIDocument;
Document doc = uidoc.Document;

// Prompt the user for the rotation angle
string angleInput = Interaction.InputBox("Enter the angle (in degrees) to rotate the model:", "Rotate Model File About Internal Origin", "0");
if (!double.TryParse(angleInput, out double rotationAngleDegrees))
{
TaskDialog.Show("Error", "Invalid angle entered. Please enter a numeric value.");
return;
}

// Convert the rotation angle to radians
double rotationAngle = rotationAngleDegrees * (Math.PI / 180.0);

// Get the internal origin point as the center of rotation
XYZ centerPoint = XYZ.Zero;

// Define the rotation axis (Z-axis)
Line rotationAxis = Line.CreateBound(centerPoint, centerPoint + XYZ.BasisZ);

// Collect all elements that can be transformed
FilteredElementCollector collector = new FilteredElementCollector(doc)
.WhereElementIsNotElementType();

List<ElementId> elementIds = new List<ElementId>();

foreach (Element element in collector)
{
// Skip certain elements that are not transformable (e.g., levels, views, phases, grids)
if (!(element is Level) && !(element is View) && !(element is Phase) && !(element is Grid))
{
elementIds.Add(element.Id);
}
}

// Start a transaction to rotate the model
using (Transaction transaction = new Transaction(doc, "Rotate Entire Model"))
{
try
{
transaction.Start();

// Rotate all collected elements around the internal origin
ElementTransformUtils.RotateElements(doc, elementIds, rotationAxis, rotationAngle);

transaction.Commit();
TaskDialog.Show("Success", string.Format("The model has been successfully rotated by {0} degrees about the internal origin.", rotationAngleDegrees));
}
catch (Exception e)
{
transaction.RollBack();
TaskDialog.Show("Error", string.Format("An error occurred while rotating the model: {0}", e.Message));
}
}
}
}
}

Screenshot 2024-12-02 091541.png

0 Likes
Message 7 of 15

Mohamed_Arshad
Advisor
Advisor

Hi @dwightduronidavy 

Kindly follow below steps.

  1. Add Microsoft.VisualBasic reference to the project
    Mohamed_Arshad_0-1733140560959.png

     

  2. Call Input Box Method and pass the parameters.

 

// Prompt the user for the rotation angle
string angleInput = Microsoft.VisualBasic.Interaction.InputBox("Enter the angle (in degrees) to rotate the model:","Rotate Model File About Internal Origin","0");​

 

 

Complete Code for Reference (Errors Resolved)

 

        public void RotateModelFileAboutIO()	
		{
			UIDocument uidoc = this.Application.ActiveUIDocument;
			Document doc = uidoc.Document;
			
			// Prompt the user for the rotation angle
			string angleInput = Microsoft.VisualBasic.Interaction.InputBox("Enter the angle (in degrees) to rotate the model:", "Rotate Model File About Internal Origin", "0");
			
			double rotationAngleDegrees;
			
			if (!double.TryParse(angleInput, out rotationAngleDegrees))
			{
					TaskDialog.Show("Error", "Invalid angle entered. Please enter a numeric value.");
					return;
			}
			
			// Convert the rotation angle to radians
			double rotationAngle = rotationAngleDegrees * (Math.PI / 180.0);

			// Get the internal origin point as the center of rotation
			XYZ centerPoint = XYZ.Zero;
			
			// Define the rotation axis (Z-axis)
			Line rotationAxis = Line.CreateBound(centerPoint, centerPoint + XYZ.BasisZ);

			// Collect all elements that can be transformed
			FilteredElementCollector collector = new FilteredElementCollector(doc)
			.WhereElementIsNotElementType();

			List<ElementId> elementIds = new List<ElementId>();
			
			foreach (Element element in collector)
			{
				// Skip certain elements that are not transformable (e.g., levels, views, phases, grids)
				if (!(element is Level) && !(element is View) && !(element is Phase) && !(element is Grid))
				{
					elementIds.Add(element.Id);
				}
			}
			
			// Start a transaction to rotate the model
			using (Transaction transaction = new Transaction(doc, "Rotate Entire Model")) 
			{
				
				try 
				{
						
					transaction.Start();
					// Rotate all collected elements around the internal origin
					ElementTransformUtils.RotateElements(doc, elementIds, rotationAxis, rotationAngle);

					transaction.Commit();
					TaskDialog.Show("Success", string.Format("The model has been successfully rotated by {0} degrees about the internal origin.", rotationAngleDegrees));
					
					
				} catch (Exception e) 
				{
					
					transaction.RollBack();
					TaskDialog.Show("Error", string.Format("An error occurred while rotating the model: {0}", e.Message));
				}
				
			}
			
		}

 

 

 Hope this will helps 🙂


Mohamed Arshad K
Software Developer (CAD & BIM)

Message 8 of 15

scgq425
Advocate
Advocate

Hi @dwightduronidavy :

this is my code , can be run in revit . 

you code problem is the reference not import

you can try this or  @Mohamed_Arshad code to test .

```

/*
 * Created by SharpDevelop.
 * User: MainUser
 * Date: 2024/12/2
 * Time: 20:57
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;

namespace A
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.DB.Macros.AddInId("928B9B42-B28E-44C3-ACD5-32E2DB224F2C")]
    public partial class ThisDocument
    {
        private void Module_Startup(object sender, EventArgs e)
        {
            RotateModelFileAboutIO();
        }

        private void Module_Shutdown(object sender, EventArgs e)
        {

        }

        #region Revit Macros generated code
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(Module_Startup);
            this.Shutdown += new System.EventHandler(Module_Shutdown);
        }
        #endregion
        
        public void RotateModelFileAboutIO()
{
UIDocument uidoc = this.Application.ActiveUIDocument;
Document doc = uidoc.Document;

// Prompt the user for the rotation angle
string angleInput = Microsoft.VisualBasic.Interaction.InputBox("Enter the angle (in degrees) to rotate the model:""Rotate Model File About Internal Origin""0");

double rotationAngleDegrees = 0.0D;

bool hasTry = double.TryParse(angleInput , out rotationAngleDegrees);

if (!hasTry){
TaskDialog.Show("Error""Invalid angle entered. Please enter a numeric value.");
return;
}


// Convert the rotation angle to radians
double rotationAngle = rotationAngleDegrees * (Math.PI / 180.0);

// Get the internal origin point as the center of rotation
XYZ centerPoint = XYZ.Zero;

// Define the rotation axis (Z-axis)
Line rotationAxis = Line.CreateBound(centerPoint, centerPoint + XYZ.BasisZ);

// Collect all elements that can be transformed
FilteredElementCollector collector = new FilteredElementCollector(doc)
.WhereElementIsNotElementType();

List<ElementId> elementIds = new List<ElementId>();

foreach (Element element in collector)
{
// Skip certain elements that are not transformable (e.g., levels, views, phases, grids)
if (!(element is Level) && !(element is View) && !(element is Phase) && !(element is Grid))
{
elementIds.Add(element.Id);
}
}

// Start a transaction to rotate the model
using (Transaction transaction = new Transaction(doc, "Rotate Entire Model"))
{
try
{
transaction.Start();

// Rotate all collected elements around the internal origin
ElementTransformUtils.RotateElements(doc, elementIds, rotationAxis, rotationAngle);

transaction.Commit();
TaskDialog.Show("Success"string.Format("The model has been successfully rotated by {0} degrees about the internal origin.", rotationAngleDegrees));
}
catch (Exception e)
{
transaction.RollBack();
TaskDialog.Show("Error"string.Format("An error occurred while rotating the model: {0}", e.Message));
}
}
}
    }
}

```

LanHui Xu 徐兰辉
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

Blog
LinkedIn
Revit/CAD Development | Steel Product Manager

EESignature

Message 9 of 15

dwightduronidavy
Enthusiast
Enthusiast

Thank you so much Mohamed. I followed your instructions (imported the VB ref) - but got the following error after executing the code:
Screenshot 2024-12-02 132812.png

After adding the two missing '}', i received the following error:
'Autodesk.Revit.ApplicationServices.Application' does not contain a definition for 'ActiveUIDocument' and no extension method 'ActiveUIDocument' accepting a first argument of type 'Autodesk.Revit.ApplicationServices.Application' could be found (are you missing a using directive or an assembly reference?) (CS1061) - C:\ProgramData\Autodesk\Revit\Macros\2024\Revit\AppHookup\DDA_CSharpModule4\Source\DDA_CSharpModule4\ThisApplication.cs:34,40

0 Likes
Message 10 of 15

Mohamed_Arshad
Advisor
Advisor

Hi @dwightduronidavy 

Try create a new project and test the code. It is executing in my end with no errors. 


Mohamed Arshad K
Software Developer (CAD & BIM)

Message 11 of 15

dwightduronidavy
Enthusiast
Enthusiast

Hi Mohamed,

This is the new application macro:

using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualBasic; // Importing Microsoft.VisualBasic for InputBox usage

namespace DDA_CSharpModule5
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.DB.Macros.AddInId("124C2055-E328-4AFD-9D93-C52935B8BE8A")]
    public partial class ThisApplication
    {
        private void Module_Startup(object sender, EventArgs e)
        {
            RotateModelFileAboutIO(); // Call RotateModelFileAboutIO on startup
        }

        private void Module_Shutdown(object sender, EventArgs e)
        {
        }

        #region Revit Macros generated code
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(Module_Startup);
            this.Shutdown += new System.EventHandler(Module_Shutdown);
        }
        #endregion

        public void RotateModelFileAboutIO()    
        {
            UIDocument uidoc = this.Application.ActiveUIDocument;
            Document doc = uidoc.Document;
            
            // Prompt the user for the rotation angle
            string angleInput = Microsoft.VisualBasic.Interaction.InputBox("Enter the angle (in degrees) to rotate the model:""Rotate Model File About Internal Origin""0");
            
            double rotationAngleDegrees;
            
            if (!double.TryParse(angleInput, out rotationAngleDegrees))
            {
                    TaskDialog.Show("Error""Invalid angle entered. Please enter a numeric value.");
                    return;
            }
            
            // Convert the rotation angle to radians
            double rotationAngle = rotationAngleDegrees * (Math.PI / 180.0);

            // Get the internal origin point as the center of rotation
            XYZ centerPoint = XYZ.Zero;
            
            // Define the rotation axis (Z-axis)
            Line rotationAxis = Line.CreateBound(centerPoint, centerPoint + XYZ.BasisZ);

            // Collect all elements that can be transformed
            FilteredElementCollector collector = new FilteredElementCollector(doc)
            .WhereElementIsNotElementType();

            List<ElementId> elementIds = new List<ElementId>();
            
            foreach (Element element in collector)
            {
                // Skip certain elements that are not transformable (e.g., levels, views, phases, grids)
                if (!(element is Level) && !(element is View) && !(element is Phase) && !(element is Grid))
                {
                    elementIds.Add(element.Id);
                }
            }
            
            // Start a transaction to rotate the model
            using (Transaction transaction = new Transaction(doc, "Rotate Entire Model")) 
            {
                
                try 
                {
                        
                    transaction.Start();
                    // Rotate all collected elements around the internal origin
                    ElementTransformUtils.RotateElements(doc, elementIds, rotationAxis, rotationAngle);

                    transaction.Commit();
                    TaskDialog.Show("Success"string.Format("The model has been successfully rotated by {0} degrees about the internal origin.", rotationAngleDegrees));
                    
                    
                } catch (Exception e) 
                {
                    
                    transaction.RollBack();
                    TaskDialog.Show("Error"string.Format("An error occurred while rotating the model: {0}", e.Message));
                }
                
            }
            
        }
        
    }
    
}
        
Error message:
'Autodesk.Revit.ApplicationServices.Application' does not contain a definition for 'ActiveUIDocument' and no extension method 'ActiveUIDocument' accepting a first argument of type 'Autodesk.Revit.ApplicationServices.Application' could be found (are you missing a using directive or an assembly reference?) (CS1061) - C:\ProgramData\Autodesk\Revit\Macros\2024\Revit\AppHookup\DDA_CSharpModule5\Source\DDA_CSharpModule5\ThisApplication.cs:34,40

0 Likes
Message 12 of 15

Mohamed_Arshad
Advisor
Advisor

Hi @dwightduronidavy 

It seems mismatching reference kindly replace code snippet at below the method.

Snippet-01

UIApplication uiapp= new UIApplication(this.Application.Application);
UIDocument uiDoc=uiapp.ActiveUIDocument;
Document doc = uidoc.Document;

Hope this will helps 🙂


Mohamed Arshad K
Software Developer (CAD & BIM)

0 Likes
Message 13 of 15

dwightduronidavy
Enthusiast
Enthusiast

Hi Mohamed, i got the below error:
dwightduronidavy_0-1733150707049.png

 

0 Likes
Message 14 of 15

Mohamed_Arshad
Advisor
Advisor
Accepted solution

Hi @dwightduronidavy 

 

Try check below code snippet

 

Code Snippet

UIApplication uiapp= new UIApplication(this.Application);
UIDocument uidoc=uiapp.ActiveUIDocument;
Document doc= uidoc.Document;

 

Hope this will helps 🙂


Mohamed Arshad K
Software Developer (CAD & BIM)

Message 15 of 15

dwightduronidavy
Enthusiast
Enthusiast

PERFECT! Thank you so very much Mohamed and  scgq425

0 Likes