Announcements

Community notifications may experience intermittent interruptions between 10–12 November during scheduled maintenance. We appreciate your patience.

Select point from form

Select point from form

Anonymous
Not applicable
753 Views
4 Replies
Message 1 of 5

Select point from form

Anonymous
Not applicable

I'm updating an old add-on. I am trying to place a TextNote at a specific location. It works fine if I supply it an XYZ point. But I cannot get it to work by using Document.Selection.Pickpoint. The code below is called from a separate form. I can't even catch an exception, revit becomes unresponsive. How would I pick a point then pass it to the TextNote.Create method? Thanks. 

 

        public void CreateText()
        {
            RevitView curView = m_revit.ActiveUIDocument.Document.ActiveView;
            //XYZ origin = new XYZ(10, 10, 0);
            XYZ orgin = m_document.Selection.PickPoint("Pick Note Location");
            ElementId defaultTypeId = m_revit.ActiveUIDocument.Document.GetDefaultElementTypeId(ElementTypeGroup.TextNoteType);
            
            TextNote note = TextNote.Create(m_revit.ActiveUIDocument.Document, curView.Id, orgin, "TEXT NOTE", defaultTypeId);

        }
0 Likes
754 Views
4 Replies
Replies (4)
Message 2 of 5

Mustafa.Salaheldin
Collaborator
Collaborator

You have to put your code in the Revit API context. This means either hide your form to set control back to Revit before calling the PickPoint function or put the PickPoint function in External Event and fire the event from the Form.

Don't forget to inclose the TextNote.Create () in a Transaction.


¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

0 Likes
Message 3 of 5

Anonymous
Not applicable

Thank you for your reply. I'm in the process of splitting up the Command and Form into separate distinct files. I hadn't been that careful about that when I was learning, and that was on R13 when you could still get away with that. Anyway, yes, I believe I have done that. But I agree that it has something to do with how I'm handling the form. The first chuck of code below is from the command and handles the transaction and initially displaying the form. I've removed a small piece regarding the expiration date. 

 

The second is from the form. There is actually quite a bit of code in the form, very little of it relating to Revit and it works fine when I have a set XYZ point. I have experimented with passing a reference to the Document to the form and getting the point from there. And the way it's currently set up, trying to get the point within the command.

 

 

Command:

 

    public class CreateTextNoteCommand : Autodesk.Revit.UI.IExternalCommand
    {
        public Autodesk.Revit.UI.UIApplication m_revit = null;
        UIDocument m_document;

        public Result Execute(Autodesk.Revit.UI.ExternalCommandData revit, ref string message, ElementSet elements)
        {
            m_revit = revit.Application;
            m_document = m_revit.ActiveUIDocument;
            
            Transaction trans = new Transaction(m_revit.ActiveUIDocument.Document, "Create Text Note");
            trans.Start();

            try
            {
                bool checkDate = CheckDate();
                if (!checkDate)
                {
                    trans.RollBack();
                    return Result.Failed;
                }

                using (CreateTextNoteForm displayForm = new CreateTextNoteForm(this))
                {
                    if (displayForm.ShowDialog() != DialogResult.OK)
                    {
                        trans.RollBack();
                        return Result.Cancelled;
                    }
                }

                trans.Commit();
                return Result.Succeeded;
            }
            catch (Exception ex)
            {
                message = ex.Message;
                trans.RollBack();
                return Result.Failed;
            }
        }

        public void CreateNote(String note)
        {
            try
            {
                RevitView curView = m_revit.ActiveUIDocument.Document.ActiveView;
                XYZ selectedPoint = m_document.Selection.PickPoint("Pick Note Location");
                ElementId defaultTypeId = m_revit.ActiveUIDocument.Document.GetDefaultElementTypeId(ElementTypeGroup.TextNoteType);

                TextNote _TextNote = TextNote.Create(
                    m_revit.ActiveUIDocument.Document,
                    curView.Id,
                    selectedPoint,
                    note,
                    defaultTypeId
                    );
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                throw;
            }
        }
    }

Form:

 

        private void button3_Click(object sender, EventArgs e)
        {
            ProcessDialog();
            this.Hide();
            //GetPoint();
            m_commandData.CreateNote(textNote);

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

textnote.PNG

 

 

0 Likes
Message 4 of 5

jeremytammik
Autodesk
Autodesk

Dear Bthatcher,

 

Thank you for your query, and thanks to Mustafa for the very valid point.

 

Look at the Revit SDK ModelessForm_ExternalEvent to see how to handle this.

 

Lots more background information is provided in The Building Coder topic group on Idling and External Events for Modeless Access and Driving Revit from Outside:

 

http://thebuildingcoder.typepad.com/blog/about-the-author.html#5.28

 

A similar issue was recently discussed in another thread:

 

http://forums.autodesk.com/t5/revit-api/managing-transaction-in-a-function-initiated-from-a-modeless...

 

I hope this helps.

 

Best regards,

 

Jeremy



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 5 of 5

Mustafa.Salaheldin
Collaborator
Collaborator

Please try this

 

In the Form:

    public partial class CreateTextNoteForm : Form
    {
        ExternalEvent m_ex = null;
        public CreateTextNoteForm(UIApplication app, ExternalEvent exEvent)
        {
            InitializeComponent();
            m_ex = exEvent;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            m_ex.Raise();
        }
    }

The External command shall be:

        public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elemSet)
        {
            _cachedCmdData = cmdData;

            try
            {
                //TODO: add your code below.

                TextInsertHandler handler = new TextInsertHandler();

                ExternalEvent exEvent = ExternalEvent.Create(handler);

                CreateTextNoteForm displayForm = new CreateTextNoteForm(cmdData.Application, exEvent);

                displayForm.Show();

                return Result.Succeeded;
            }
            catch (Exception ex)
            {
                msg = ex.ToString();
                return Result.Failed;
            }
        }

        public class TextInsertHandler : IExternalEventHandler
        {
            public String GetName()
            {
                return "R2014 External Event Sample";
            }
            public void Execute(UIApplication uiapp)
            {
                try
                {
                    using (Transaction trans = new Transaction(uiapp.ActiveUIDocument.Document, "Create Text Note"))
                    {
                        trans.Start();

                        CreateNote("test");

                        trans.Commit();
                    }
                }
                catch (Exception ex) { }

                return;
            }

        }  // class

        public static void CreateNote(String note)
        {
            try
            {
                Autodesk.Revit.DB.View curView = _cachedCmdData.Application.ActiveUIDocument.ActiveView;
                XYZ selectedPoint = _cachedCmdData.Application.ActiveUIDocument.Selection.PickPoint("Pick Note Location");
                ElementId defaultTypeId = _cachedCmdData.Application.ActiveUIDocument.Document.GetDefaultElementTypeId(ElementTypeGroup.TextNoteType);

                TextNote _TextNote = TextNote.Create(
                    _cachedCmdData.Application.ActiveUIDocument.Document,
                    curView.Id,
                    selectedPoint,
                    note,
                    defaultTypeId
                    );
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                throw;
            }
        }

If this is the solution you seek please mark this reply as an answer


¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

0 Likes