Você está aqui: Revit C# ::: Dicas & Truques ::: Revit C# Windows Forms |
Como criar um formulário Windows Forms a partir do Revit C#Quantidade de visualizações: 624 vezes |
Nesta dica mostrarei os passos necessários para se criar uma aplicação Revit C# contendo uma formulário Windows Forms. O objetivo é demonstrar como o usuário pode interagir com os elementos do formulário para acessar e manipular objetos do Revit a partir de códigos C#. Antes de continuarmos, veja a janela Windows Forms que criamos logo abaixo: ![]() Veja que temos um formulário contendo 6 Labels e 6 TextBox contendo as coordenadas inicias e finais de uma linha geométrica usada para criar um novo eixo na área de desenho do Revit. Ao informar as coordenadas e clicar o botão Desenhar Eixo, um novo objeto da classe Grid da Revit API é criado e desenhado no documento atual. Este código é muito útil para iniciantes em programação Revit C# porque mostra como acessar o documento ativo usando ActiveUIDocument e em seguida passar este objeto para uma outra classe C#. De posse deste objeto nós podemos realizar várias tarefas, entre elas criar uma nova linha geométrica usando Line.CreateBound() e, em seguida, criar um novo Grid usando Grid.Create(). Veja o código completo para uma macro chamada Estudos do tipo Aplicativo. Se tiver dificuldades para executar, chama a gente nos contatos na parte superior do site que teremos prazer em ajudá-lo. Eis a listagem do código C#: ---------------------------------------------------------------------- Precisa de ajuda? Chama no WhatsApp +55 (62) 98553-6711 (Osmar) Este código foi útil? Paga um cafezinho pra mim :-( PIX: osmar@arquivodecodigos.com.br ---------------------------------------------------------------------- using System; using Autodesk.Revit.UI; using Autodesk.Revit.DB; using Autodesk.Revit.UI.Selection; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace Estudos { [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes. TransactionMode.Manual)] [Autodesk.Revit.DB.Macros.AddInId("ED8EC6C4-9489-48F7-B04E-B45B5D1BEB12")] public partial class ThisApplication { private void Module_Startup(object sender, EventArgs e) { JanelaPrincipal janela = new JanelaPrincipal(); janela.uidoc = this.ActiveUIDocument; janela.ShowDialog(); } private void Module_Shutdown(object sender, EventArgs e) { // para fazer alguma limpeza de memória ou algo assim } #region Revit Macros generated code private void InternalStartup() { this.Startup += new System.EventHandler(Module_Startup); this.Shutdown += new System.EventHandler(Module_Shutdown); } #endregion } // classe para representar o formulário public class JanelaPrincipal : System.Windows.Forms.Form { public UIDocument uidoc; public System.Windows.Forms.TextBox txtX0; public System.Windows.Forms.TextBox txtY0; public System.Windows.Forms.TextBox txtZ0; public System.Windows.Forms.TextBox txtX1; public System.Windows.Forms.TextBox txtY1; public System.Windows.Forms.TextBox txtZ1; // construtor da classe public JanelaPrincipal() { this.Text = "Meu Formulário Revit C# Windows Forms"; // título da janela this.Width = 390; // largura da janela this.Height = 240; // altura da janela // define a posição atual da janela this.StartPosition = FormStartPosition.CenterScreen; // vamos criar quatro labels e quatro caixas de texto Label label1 = new Label(); label1.Text = "X Inicial:"; label1.Location = new System.Drawing.Point(20, 20); label1.Height = 15; label1.Width = 80; Label label2 = new Label(); label2.Text = "Y Inicial:"; label2.Location = new System.Drawing.Point(120, 20); label2.Height = 15; label2.Width = 80; Label label3 = new Label(); label3.Text = "Z Inicial:"; label3.Location = new System.Drawing.Point(220, 20); label3.Height = 15; label3.Width = 80; txtX0 = new System.Windows.Forms.TextBox(); txtX0.Location = new System.Drawing.Point(20, 40); txtX0.Width = 80; txtX0.Height = 15; txtY0 = new System.Windows.Forms.TextBox(); txtY0.Location = new System.Drawing.Point(120, 40); txtY0.Width = 80; txtY0.Height = 15; txtZ0 = new System.Windows.Forms.TextBox(); txtZ0.Location = new System.Drawing.Point(220, 40); txtZ0.Width = 80; txtZ0.Height = 15; Label label4 = new Label(); label4.Text = "X Final:"; label4.Location = new System.Drawing.Point(20, 80); label4.Height = 15; label4.Width = 80; Label label5 = new Label(); label5.Text = "Y Final:"; label5.Location = new System.Drawing.Point(120, 80); label5.Height = 15; label5.Width = 80; Label label6 = new Label(); label6.Text = "Z Final:"; label6.Location = new System.Drawing.Point(220, 80); label6.Height = 15; label6.Width = 80; txtX1 = new System.Windows.Forms.TextBox(); txtX1.Location = new System.Drawing.Point(20, 97); txtX1.Width = 80; txtX1.Height = 15; txtY1 = new System.Windows.Forms.TextBox(); txtY1.Location = new System.Drawing.Point(120, 97); txtY1.Width = 80; txtY1.Height = 15; txtZ1 = new System.Windows.Forms.TextBox(); txtZ1.Location = new System.Drawing.Point(220, 97); txtZ1.Width = 80; txtZ1.Height = 15; // botão que desenha o eixo nas coordenadas indicadas Button button1 = new Button(); button1.Text = "Desenhar Eixo"; button1.Location = new System.Drawing.Point(95, 160); button1.Width = 150; button1.Click += new System.EventHandler(DesenharEixo); // botão que fecha a janela Button button2 = new Button(); button2.Text = "Fechar"; button2.Location = new System.Drawing.Point(265, 160); button2.Width = 80; button2.Click += new System.EventHandler(FecharJanela); // adiciona os controles à janela this.Controls.Add(label1); this.Controls.Add(label2); this.Controls.Add(label3); this.Controls.Add(label4); this.Controls.Add(label5); this.Controls.Add(label6); this.Controls.Add(txtX0); this.Controls.Add(txtY0); this.Controls.Add(txtZ0); this.Controls.Add(txtX1); this.Controls.Add(txtY1); this.Controls.Add(txtZ1); this.Controls.Add(button1); this.Controls.Add(button2); } // função usada para desenhar um novo eixo no documento atual private void DesenharEixo(object sender, System.EventArgs e) { // primeiro obtemos uma referência ao documento atual Document doc = uidoc.Document; // criamos a linha geométrica para posicionar o eixo XYZ inicio = new XYZ(Double.Parse(txtX0.Text), Double.Parse(txtY0.Text), Double.Parse(txtZ0.Text)); XYZ final = new XYZ(Double.Parse(txtX1.Text), Double.Parse(txtY1.Text), Double.Parse(txtZ1.Text)); // construímos a linha Line linhaGeometrica = Line.CreateBound(inicio, final); // iniciamos uma nova transação using(Transaction t= new Transaction(doc)) { t.Start("Criar um novo Grid"); Grid grid = Grid.Create(doc, linhaGeometrica); t.Commit(); } } // função usada para fechar a janela private void FecharJanela(object sender, System.EventArgs e) { this.Close(); } } } Não se esqueça de adicionar referências a System.Windows.Forms e System.Drawing. Basta, dentro do SharpDevelop, ir no menu Projeto -> Adicionar Referência. |
![]() |
Veja mais Dicas e truques de Revit C# |
Dicas e truques de outras linguagens |
GNU Octave - Como calcular o comprimento da hipotenusa em GNU Octave dadas as medidas do cateto oposto e do cateto adjascente |
E-Books em PDF |
||||
|
||||
|
||||
Linguagens Mais Populares |
||||
1º lugar: Java |