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

0 Members and 1 Guest are viewing this topic.

Coder

  • Swamp Rat
  • Posts: 827
Use WPF dialog button to draw a line
« on: May 18, 2015, 07:24:24 AM »
Hello guys .

I am just learning how to use the WPF in AutoCAD and hope someone could help me to learn how to do this process to allow me to do future programs on my own later on.

This is the simple dialog with one button entitled "Draw Line"

I need to press the button then the program should draw the line from 0,0,0 to 10,10,0

What is the format that I should build the program with to be able to run it in Autocad ?

Many thanks in advance

Code - C#: [Select]
  1. <Window x:Class="WpfApplication1.MainWindow"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         Title="MainWindow" Height="100" Width="250" HorizontalAlignment="Left" VerticalAlignment="Top">
  5.     <Grid Margin="0,0,-8,-3">
  6.         <Button x:Name="Draw" Content="Draw Line" HorizontalAlignment="Left" Margin="65,18,0,0" VerticalAlignment="Top" Width="100" Height="30" Click="Draw_Click"/>
  7.     </Grid>
  8.  

And this is the C# codes that I use to draw a line via dll files .

Code - C#: [Select]
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Text;
  6. using Autodesk.AutoCAD.Runtime;
  7. using Autodesk.AutoCAD.ApplicationServices;
  8. using Autodesk.AutoCAD.DatabaseServices;
  9. using Autodesk.AutoCAD.Geometry;
  10. using App = Autodesk.AutoCAD.ApplicationServices.Application;
  11.  
  12. namespace WindowsFormsApplication1
  13. {
  14.     class DrawLine
  15.     {
  16.         [CommandMethod("Drline")]
  17.         static public void line()
  18.         {
  19.             Document acDoc = App.DocumentManager.MdiActiveDocument;
  20.             Database acCurDb = acDoc.Database;
  21.             using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
  22.             {
  23.                 BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
  24.                 BlockTableRecord acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
  25.                 Line acLine = new Line(new Point3d(0, 0, 0), new Point3d(10, 10, 0));
  26.                 acBlkTblRec.AppendEntity(acLine);
  27.                 acTrans.AddNewlyCreatedDBObject(acLine, true);
  28.                 acTrans.Commit();
  29.             }
  30.         }
  31.     }
  32. }
  33.  

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Use WPF dialog button to draw a line
« Reply #1 on: May 18, 2015, 07:35:43 AM »
What is the format that I should build the program with to be able to run it in Autocad ?
Managed DLL, like your usual managed .net extension.

Coder

  • Swamp Rat
  • Posts: 827
Re: Use WPF dialog button to draw a line
« Reply #2 on: May 18, 2015, 08:29:17 AM »
What is the format that I should build the program with to be able to run it in Autocad ?
Managed DLL, like your usual managed .net extension.

Hi ,

When I built the dll from the menu BUILD -> Build Solution I moved to autocad to run the program , I have found many dll files and they are the assemblies that included in the program  :-o

Which one that represents the program ? although the name of the program is WpfApplication1

BlackBox

  • King Gator
  • Posts: 3770
Re: Use WPF dialog button to draw a line
« Reply #3 on: May 18, 2015, 08:46:35 AM »
Assembly References, except for with rare exception, should have Copy Local == False.
"How we think determines what we do, and what we do determines what we get."

Coder

  • Swamp Rat
  • Posts: 827
Re: Use WPF dialog button to draw a line
« Reply #4 on: May 18, 2015, 08:54:13 AM »
Assembly References, except for with rare exception, should have Copy Local == False.
Thank you BlackBox

I turned all references to False and did not have any dll in the debug folder  :-(

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Use WPF dialog button to draw a line
« Reply #5 on: May 18, 2015, 08:59:21 AM »
Coder... Be not lazy...  :-( RTFM. The answers of your questions can be looked for at the first pages of manuals often. Reading documentation is a need of each programmer. Don't expect that someone will do it for you (friendly advice).
« Last Edit: May 18, 2015, 09:06:47 AM by Andrey Bushman »

Coder

  • Swamp Rat
  • Posts: 827
Re: Use WPF dialog button to draw a line
« Reply #6 on: May 18, 2015, 09:15:47 AM »
Coder... Be not lazy...  :-( RTFM. The answers of your questions can be looked for at the first pages of manuals often. Reading documentation is a need of each programmer. Don't expect that someone will do it for you (friendly advice).

I am not Lazy Andrey , I am just asking for specific thing that I can't find elsewhere and the Help document for the .net is difficult to deal with and that is what I am facing at the current time .

This is the first time I am working with dialog in C# and we should expect mistakes like this  , right ?

Can you please tell me where can I find the answer for my first question in the Help document ?

Thank
« Last Edit: May 18, 2015, 09:27:50 AM by Coder »

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Use WPF dialog button to draw a line
« Reply #7 on: May 18, 2015, 09:28:14 AM »
Quote
I turned all references to False and did not have any dll in the debug folder  :-(
You don't need all DLL files which you pointed in your screen. From my previous link:
Quote
The main DLL files of the AutoCAD .NET API that you will frequently use are:
  • AcCoreMgd.dll. Use when working within the editor, publishing and plotting, and defining commands and functions that can be called from AutoLISP.
  • AcDbMgd.dll. Use when working with objects stored in a drawing file.
  • AcMgd.dll. Use when working with the application and user interface.
  • AcCui.dll. Use when working with customization files.
...
Once a AutoCAD .NET API DLL is referenced, you must set the Copy Local property of the referenced DLL to False.
It [Copy Local] means for DLL of Object SDK only, instead of all DLL files of your project. Also your screen has nothing of your output DLL (it has DLL of SDK only).

Check the "Your project properties -> Application tab -> Output type". It must to be "Class Library".
« Last Edit: May 18, 2015, 09:38:04 AM by Andrey Bushman »

Coder

  • Swamp Rat
  • Posts: 827
Re: Use WPF dialog button to draw a line
« Reply #8 on: May 18, 2015, 09:37:56 AM »
You don't need all DLL files which you pointed in your screen. From my previous link:

They came out to debug folder automatically after I build the program and I am not the one who copied them .


Coder

  • Swamp Rat
  • Posts: 827
Re: Use WPF dialog button to draw a line
« Reply #9 on: May 18, 2015, 09:42:11 AM »
Check the "Your project properties -> Application tab -> Output type". It must to be "Class Library".

After changing it to class library and build the program , it throws errors .
Why should I change it to Class library and I am working with windows dialogs ?.

Andrey Bushman

  • Swamp Rat
  • Posts: 864
Re: Use WPF dialog button to draw a line
« Reply #10 on: May 18, 2015, 09:47:03 AM »
They came out to debug folder automatically after I build the program and I am not the one who copied them .
Only cats are born automatically themselves. :) That files "appeared" because their "Copy Local" properties was True by default. You can delete them from your output directory manually, if they doesn't disappear when you launch rebuilding.

Quote from: Coder
Why should I change it to Class library and I am working with windows dialogs ?.
Because you write a .net extension for AutoCAD instead of autonomic application.
Quote from: Coder
After changing it to class library and build the program , it throws errors .
What is "it"? IDE or AutoCAD (compilation or runtime error)? Are you waiting, we will guess the text of your errors? :) Where is the text of your error messages?

Also, don't forget the .NET Framework version of your project must be the same which can be used by your AutoCAD version.
« Last Edit: May 18, 2015, 10:11:51 AM by Andrey Bushman »

BlackBox

  • King Gator
  • Posts: 3770
Re: Use WPF dialog button to draw a line
« Reply #11 on: May 18, 2015, 11:09:46 AM »
Perhaps you should consider using the AutoCAD .NET Wizard, until you're more familiar with configuring your own Solutions, Projects, etc., as it (the Wizard) will do much of the basics for you.


Cheers
"How we think determines what we do, and what we do determines what we get."

ChrisCarlson

  • Guest
Re: Use WPF dialog button to draw a line
« Reply #12 on: May 18, 2015, 12:09:19 PM »
Perhaps you should consider using the AutoCAD .NET Wizard, until you're more familiar with configuring your own Solutions, Projects, etc., as it (the Wizard) will do much of the basics for you.


Cheers

What he said! Just note that I don't think it has been updated for post VS 2012 yet.

BlackBox

  • King Gator
  • Posts: 3770
Re: Use WPF dialog button to draw a line
« Reply #13 on: May 18, 2015, 12:18:30 PM »
Perhaps you should consider using the AutoCAD .NET Wizard, until you're more familiar with configuring your own Solutions, Projects, etc., as it (the Wizard) will do much of the basics for you.


Cheers

What he said! Just note that I don't think it has been updated for post VS 2012 yet.

*Tips hat*

FWIW - I use it (the Wizard, albeit a customized version of) + Visual Studio 2013 to develop for 2012-2016 products.
"How we think determines what we do, and what we do determines what we get."

Coder

  • Swamp Rat
  • Posts: 827
Re: Use WPF dialog button to draw a line
« Reply #14 on: May 18, 2015, 01:40:28 PM »
Perhaps you should consider using the AutoCAD .NET Wizard, until you're more familiar with configuring your own Solutions, Projects, etc., as it (the Wizard) will do much of the basics for you.


Cheers

Hi,
Autocad .net Wizard is very helpful and easy to deal with than WPF and Windows forms .

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 ?

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Use WPF dialog button to draw a line
« Reply #30 on: May 21, 2015, 12:58:24 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 ?

I have no idea why, it should work if you picked pt1 in the first part of the method, that point is set as the base point for the rubber band. Does your code look like this?
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; // ******* this point gets used as the base point in the next part
  10.             if (rep1.Status == PromptStatus.Cancel) return;
  11.  
  12.             PromptPointOptions p2 = new PromptPointOptions("Next point :");
  13.             p2.UseBasePoint = true; // ******* yes, I want to use a rubber band
  14.             p2.BasePoint = pt1; // ******* use this point as the base point for the rubber band line
  15.             PromptPointResult rep2;
  16.             rep2 = doc.Editor.GetPoint(p2);
  17.             Point3d pt2 = rep2.Value;
  18.             if (rep2.Status == PromptStatus.Cancel) return;
  19.  
  20.             using (Transaction tr = db.TransactionManager.StartTransaction())
  21.             {
  22.                 var mSpace = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite);
  23.                 var line = new Line(pt1, pt2);
  24.                 line.ColorIndex = 6;
  25.  
  26.                 mSpace.AppendEntity(line);
  27.                 tr.AddNewlyCreatedDBObject(line, true);
  28.                 tr.Commit();
  29.             }
  30.         }
  31.  
« Last Edit: May 21, 2015, 01:04:40 AM by MickD »
"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 #31 on: May 21, 2015, 01:08:12 AM »

I have no idea why, it should work if you picked pt1 in the first part of the method, that point is set as the base point for the rubber band. Does your code look like this?

Yes the same . and I replaced your modified codes with my current codes and it gives the same result . :-o

Coder

  • Swamp Rat
  • Posts: 827
Re: Use WPF dialog button to draw a line
« Reply #32 on: May 21, 2015, 01:13:03 AM »
Sorry I replied before you modified the codes .

Now it works  :-) thank you .

What was the problem in the first codes according to this last modified codes ?

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Use WPF dialog button to draw a line
« Reply #33 on: May 21, 2015, 01:16:58 AM »
Without looking at your code I have no idea and if you have a look they are exactly the same lines of code I posted earlier, I just put them in the method listed in the post above.

You really need to study what you did and work it out, it was probably something simple, it always is. If you don't find the problem you'll never know :)
"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 #34 on: May 21, 2015, 09:22:44 AM »
Thank you MickD for your help and advice .