Author Topic: Use WPF dialog button to draw a line  (Read 8653 times)

0 Members and 1 Guest are viewing this topic.

Coder

  • Swamp Rat
  • Posts: 827
Re: Use WPF dialog button to draw a line
« Reply #15 on: May 18, 2015, 01:42:16 PM »
14 posts and we still did not nail the purpose of the thread   :-(

BlackBox

  • King Gator
  • Posts: 3770
Re: Use WPF dialog button to draw a line
« Reply #16 on: May 18, 2015, 01:50:06 PM »
14 posts and we still did not nail the purpose of the thread   :-(

Sorry, Coder; we've been sidetracked with helping you properly compile your project, configure assembly reference, etc.

As an aside, perhaps you should consider looking closely at (if not posting?) your Draw_Click event handler, rather than posting big letters. Just saying.
"How we think determines what we do, and what we do determines what we get."

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Use WPF dialog button to draw a line
« Reply #17 on: May 18, 2015, 04:09:15 PM »
Hi,

As you had been advised, you should familiarize yourself with the .NET environment outside AutoCAD before attempting to use the AutoCAD API.

Anyway, IMO a simple way should be, in a single project (class library) referencing the AutoCAD libraries with a class for the AutoCAD commands, to add a WPF window (right click the project in the solution explorer > Add > Window...).
So you'd have on one side the dialog described in the Window1.xaml and the code behind in window1.xaml.cs to define the click event handler and, on the other side a class related to AutoCAD where you'd define a command to show the dialog and a static method to draw the line.

The Window1.xaml file
Code - XML: [Select]
  1. <Window x:Class="WpfDialogSample.Window1"
  2.        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.        Title="Window1" Height="100" Width="200">
  5.     <Grid>
  6.         <Button Content="Draw Line" Margin="10" Width="100" Height="30" Click="Draw_Click"/>
  7.     </Grid>
  8. </Window>

The code behind (Window1.xaml.cs)
Code - C#: [Select]
  1. using System.Windows;
  2.  
  3. namespace WpfDialogSample
  4. {
  5.     public partial class Window1 : Window
  6.     {
  7.         public Window1()
  8.         {
  9.             InitializeComponent();
  10.         }
  11.  
  12.         private void Draw_Click(object sender, RoutedEventArgs e)
  13.         {
  14.             CommandMethods.DrawLine();
  15.             this.Close();
  16.         }
  17.     }
  18. }

The CommandMethod class:
Code - C#: [Select]
  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using Autodesk.AutoCAD.Geometry;
  4. using Autodesk.AutoCAD.Runtime;
  5. using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
  6.  
  7. [assembly: CommandClass(typeof(WpfDialogSample.CommandMethods))]
  8.  
  9. namespace WpfDialogSample
  10. {
  11.     public class CommandMethods
  12.     {
  13.         // AutoCAD command to display the dialog
  14.         [CommandMethod("TEST", CommandFlags.Modal)]
  15.         public static void Test()
  16.         {
  17.             System.Windows.Window win = new Window1();
  18.             AcAp.ShowModalWindow(win);
  19.         }
  20.  
  21.         // draws a line in model space
  22.         internal static void DrawLine()
  23.         {
  24.             Document doc = AcAp.DocumentManager.MdiActiveDocument;
  25.             Database db = doc.Database;
  26.             using (Transaction tr = db.TransactionManager.StartTransaction())
  27.             {
  28.                 var mSpace = (BlockTableRecord)tr.GetObject(
  29.                     SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite);
  30.                 var line = new Line(Point3d.Origin, new Point3d(10.0, 10.0, 0.0));
  31.                 mSpace.AppendEntity(line);
  32.                 tr.AddNewlyCreatedDBObject(line, true);
  33.                 tr.Commit();
  34.             }
  35.         }
  36.     }
  37. }
Speaking English as a French Frog

Coder

  • Swamp Rat
  • Posts: 827
Re: Use WPF dialog button to draw a line
« Reply #18 on: May 19, 2015, 01:09:15 AM »
Thank you gile for that hard work .

Everything works great so far , but one question about the position of the dialog please .
The dialog starts at the position ( Up Left Hand side ) of the screen , can we control of this property to position the dialog in the center of the screen for example ?

Many thanks gile.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Use WPF dialog button to draw a line
« Reply #19 on: May 19, 2015, 02:00:50 AM »
You can use the WindowStartupLocation attribute in the Xaml Window tag (CenterScreen, CenterOwner).
Speaking English as a French Frog

Coder

  • Swamp Rat
  • Posts: 827
Re: Use WPF dialog button to draw a line
« Reply #20 on: May 19, 2015, 02:31:00 AM »
Very good gile , thank you so much  :-)

Coder

  • Swamp Rat
  • Posts: 827
Re: Use WPF dialog button to draw a line
« Reply #21 on: May 19, 2015, 08:54:46 AM »
Hello guys .

I just added a few lines of codes to specify two points on the screen to draw a line but I see the dialog appear and disappear after picking the first and the second point , how to fix this error ?

Thanks in advance  :-)

Code - C#: [Select]
  1.         internal static void DrawLine()
  2.         {
  3.             Document doc = AcAp.DocumentManager.MdiActiveDocument;
  4.             Database db = doc.Database;
  5.  
  6.             PromptPointOptions p1 = new PromptPointOptions("Specify base point :");
  7.             PromptPointResult rep1;
  8.             rep1 = doc.Editor.GetPoint(p1);
  9.             Point3d pt1 = rep1.Value;
  10.             if (rep1.Status == PromptStatus.Cancel) return;
  11.  
  12.             PromptPointOptions p2 = new PromptPointOptions("Next point :");
  13.             PromptPointResult rep2;
  14.             rep2 = doc.Editor.GetPoint(p2);
  15.             Point3d pt2 = rep2.Value;
  16.             if (rep2.Status == PromptStatus.Cancel) return;
  17.  
  18.             using (Transaction tr = db.TransactionManager.StartTransaction())
  19.             {
  20.                 var mSpace = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite);
  21.                 var line = new Line(pt1, pt2);
  22.                 line.ColorIndex = 6;
  23.                
  24.                 mSpace.AppendEntity(line);
  25.                 tr.AddNewlyCreatedDBObject(line, true);
  26.                 tr.Commit();
  27.             }
  28.         }

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Use WPF dialog button to draw a line
« Reply #22 on: May 19, 2015, 09:08:03 AM »

Coder

  • Swamp Rat
  • Posts: 827
Re: Use WPF dialog button to draw a line
« Reply #23 on: May 19, 2015, 09:26:43 AM »
It maybe interesting for you.

The same result , the dialog appears quickly and disappear after selecting an object so the problem still the same  :-(

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Use WPF dialog button to draw a line
« Reply #24 on: May 19, 2015, 10:01:50 AM »
This is the standard behavior of a modal dialog to hide themselves during user interaction.
In your case, you can simply close the dialog as soon as the user click the button and call DrawLine after.

Code - C#: [Select]
  1.         private void Draw_Click(object sender, RoutedEventArgs e)
  2.         {
  3.             this.Close();
  4.             CommandMethods.DrawLine();
  5.         }

Another way should be using a modeless dialog, but it's much more resposability (locking document, giving focus, ...) and probably over your head for the moment.
« Last Edit: May 19, 2015, 11:28:04 AM by gile »
Speaking English as a French Frog

Coder

  • Swamp Rat
  • Posts: 827
Re: Use WPF dialog button to draw a line
« Reply #25 on: May 19, 2015, 03:50:06 PM »
Thanks a lot gile , that did the job nicely  :-)

Would the rubber band be difficult to add to the program while the user is picking the first point and the second one ?

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Use WPF dialog button to draw a line
« Reply #26 on: May 19, 2015, 04:06:37 PM »
Look at the PromptPointOptions.BasePoint and PromptPointOptions.UseBasePoint properties.
Speaking English as a French Frog

Coder

  • Swamp Rat
  • Posts: 827
Re: Use WPF dialog button to draw a line
« Reply #27 on: May 20, 2015, 08:50:43 AM »
Hello ,

I found this codes from the help file but it is different that the way that I wrote my codes .
Should I follow the way that the down posted codes written ?

I hope someone can modify my codes posted in reply # 21 without following codes below , is this possible ?

Thank you .

Code - C#: [Select]
  1.                 PromptPointOptions pPtOpts = new PromptPointOptions("");
  2.                 PromptPointResult pPtRes;
  3.                 // Prompt for the start point
  4.                 pPtOpts.Message = "\nEnter the start point of the line: ";
  5.                 pPtRes = doc.Editor.GetPoint(pPtOpts);
  6.                 Point3d ptStart = pPtRes.Value;
  7.                 // Exit if the user presses ESC or cancels the command
  8.                 if (pPtRes.Status == PromptStatus.Cancel) return;
  9.                 // Prompt for the end point
  10.                 pPtOpts.Message = "\nEnter the end point of the line: ";
  11.                 pPtOpts.UseBasePoint = true;
  12.                 pPtOpts.BasePoint = ptStart;
  13.                 pPtRes = doc.Editor.GetPoint(pPtOpts);
  14.                 Point3d ptEnd = pPtRes.Value;
  15.  

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Use WPF dialog button to draw a line
« Reply #28 on: May 20, 2015, 05:27:34 PM »
you need to set up your PromptPointOptions once you create the instance:

Code - C#: [Select]
  1. PromptPointOptions p2 = new PromptPointOptions("Next point :");
  2. p2.UseBasePoint = true; // yes, I want to use a rubber band
  3. p2.BasePoint = pt1; // use this point as the base point for the rubber band line
  4.  

Add those 2 lines and you should be right to go.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

Coder

  • Swamp Rat
  • Posts: 827
Re: Use WPF dialog button to draw a line
« Reply #29 on: May 21, 2015, 12:24:28 AM »
Thank you MickD for your reply .

After picking the first point the rubber band goes to 0,0,0 and did not start from first point  :-( can you check this out again please ?