Author Topic: Link a dialog with class file  (Read 2222 times)

0 Members and 1 Guest are viewing this topic.

Coder

  • Swamp Rat
  • Posts: 827
Link a dialog with class file
« on: April 16, 2015, 06:49:34 AM »
Hello guys .

I need to link a dialog with one button to draw a line with the codes that written in Class file , how can I do that ?

Class file codes .

Code - vb.net: [Select]
  1. Imports Autodesk.AutoCAD.Runtime
  2. Imports Autodesk.AutoCAD.ApplicationServices
  3. Imports Autodesk.AutoCAD.DatabaseServices
  4. Imports Autodesk.AutoCAD.Geometry
  5.  
  6. Namespace myspace
  7.     Public Class rocks
  8.         <CommandMethod("AddLine")>
  9.         Public Sub AddLine()
  10.             '' Get the current document and database
  11.             Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
  12.             Dim acCurDb As Database = acDoc.Database
  13.             '' Start a transaction
  14.             Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
  15.                 '' Open the Block table for read
  16.                 Dim acBlkTbl As BlockTable
  17.                 acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
  18.                 '' Open the Block table record Model space for write
  19.                 Dim acBlkTblRec As BlockTableRecord
  20.                 acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace),  OpenMode.ForWrite)
  21.                 Dim acLine As Line = New Line(New Point3d(0, 0, 0), New Point3d(12, 3, 0))
  22.                 acLine.SetDatabaseDefaults()
  23.                 acLine.ColorIndex = 1
  24.                 Dim ltt As LinetypeTable
  25.                 ltt = acTrans.GetObject(acCurDb.LinetypeTableId, OpenMode.ForRead)
  26.                 If Not ltt.Has("HIDDEN") Then
  27.                     acCurDb.LoadLineTypeFile("HIDDEN", "acad.lin")
  28.                 End If
  29.                 acLine.Linetype = "HIDDEN"
  30.                 acLine.LineWeight = LineWeight.LineWeight040
  31.                 '' Add the new object to the block table record and the transaction
  32.                 acBlkTblRec.AppendEntity(acLine)
  33.                 acTrans.AddNewlyCreatedDBObject(acLine, True)
  34.                 '' Save the new object to the database
  35.                 acTrans.Commit()
  36.             End Using
  37.         End Sub
  38.     End Class
  39. End Namespace
  40.  
  41.  

Dialog form codes .

I tried as shown below but it still doesn't work.

Code - vb.net: [Select]
  1. Public Class Form1 (  )
  2.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  3.         Dim a As myspace.rocks = New myspace.rocks
  4.         a.AddLine()
  5.  
  6.     End Sub
  7. End Class
  8.  
« Last Edit: April 16, 2015, 07:57:45 AM by Coder »

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Link a dialog with class file
« Reply #1 on: April 16, 2015, 09:29:46 AM »
Just to emphasize my comments on your other post. I'm going to show you using the most simple C#, WPF, and MVVM example. You commented on just getting the engine started.  I say why learn to drive a Yugo first when the Corvette is sitting right beside it with the keys in it.  If you're going to take the time and learn a new skill don't waste your time having to learn the basics twice.

The ViewModel
Code - C#: [Select]
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.ComponentModel;
  4. using System.Runtime.CompilerServices;
  5. using System.Windows.Input;
  6. using Autodesk.AutoCAD.DatabaseServices;
  7. using Autodesk.AutoCAD.Geometry;
  8. using LineExample.Annotations;
  9. using Application = Autodesk.AutoCAD.ApplicationServices.Core.Application;
  10.  
  11. namespace LineExample
  12. {
  13.     public class ViewModel : INotifyPropertyChanged
  14.     {
  15.         public event PropertyChangedEventHandler PropertyChanged;
  16.  
  17.         private double _startPointX;
  18.         private double _startPointY;
  19.         private double _startPointZ;
  20.         private double _endPointX;
  21.         private double _endPointY;
  22.         private double _endPointZ;
  23.         private LineWeight _lineWeight;
  24.         private ICommand _createLineCommand;
  25.  
  26.         public double StartPointX
  27.         {
  28.             get { return _startPointX; }
  29.             set
  30.             {
  31.                 if (value.Equals(_startPointX)) return;
  32.                 _startPointX = value;
  33.                 OnPropertyChanged();
  34.             }
  35.         }
  36.  
  37.         public double StartPointY
  38.         {
  39.             get { return _startPointY; }
  40.             set
  41.             {
  42.                 if (value.Equals(_startPointY)) return;
  43.                 _startPointY = value;
  44.                 OnPropertyChanged();
  45.             }
  46.         }
  47.  
  48.         public double StartPointZ
  49.         {
  50.             get { return _startPointZ; }
  51.             set
  52.             {
  53.                 if (value.Equals(_startPointZ)) return;
  54.                 _startPointZ = value;
  55.                 OnPropertyChanged();
  56.             }
  57.         }
  58.  
  59.         public double EndPointX
  60.         {
  61.             get { return _endPointX; }
  62.             set
  63.             {
  64.                 if (value.Equals(_endPointX)) return;
  65.                 _endPointX = value;
  66.                 OnPropertyChanged();
  67.             }
  68.         }
  69.  
  70.         public double EndPointY
  71.         {
  72.             get { return _endPointY; }
  73.             set
  74.             {
  75.                 if (value.Equals(_endPointY)) return;
  76.                 _endPointY = value;
  77.                 OnPropertyChanged();
  78.             }
  79.         }
  80.  
  81.         public double EndPointZ
  82.         {
  83.             get { return _endPointZ; }
  84.             set
  85.             {
  86.                 if (value.Equals(_endPointZ)) return;
  87.                 _endPointZ = value;
  88.                 OnPropertyChanged();
  89.             }
  90.         }
  91.  
  92.         public LineWeight LineWeight
  93.         {
  94.             get { return _lineWeight; }
  95.             set
  96.             {
  97.                 if (value == _lineWeight) return;
  98.                 _lineWeight = value;
  99.                 OnPropertyChanged();
  100.             }
  101.         }
  102.  
  103.         public ICommand CreateLineCommand
  104.         {
  105.             get { return _createLineCommand ?? (_createLineCommand = new RelayCommand(CreateLine)); }
  106.         }
  107.  
  108.         private void CreateLine(object obj)
  109.         {
  110.             var db = Application.DocumentManager.MdiActiveDocument.Database;
  111.  
  112.             using (var tr = db.TransactionManager.StartTransaction())
  113.             {
  114.                 var blockTable = (BlockTable) tr.GetObject(db.BlockTableId, OpenMode.ForRead);
  115.                 var modelSpace = (BlockTableRecord) tr.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  116.  
  117.                 var startPoint = new Point3d(StartPointX, StartPointY, StartPointZ);
  118.                 var endPoint = new Point3d(EndPointX, EndPointY, EndPointZ);
  119.                 var line = new Line(startPoint, endPoint);
  120.                 line.SetDatabaseDefaults(db);
  121.                 line.ColorIndex = 1;
  122.  
  123.                 var lineTypeTable = (LinetypeTable) tr.GetObject(db.LinetypeTableId, OpenMode.ForRead);
  124.                 if (!lineTypeTable.Has("HIDDEN"))
  125.                     db.LoadLineTypeFile("HIDDEN", "acad.lin");
  126.  
  127.                 line.Linetype = "HIDDEN";
  128.                 line.LineWeight = LineWeight;
  129.  
  130.                 modelSpace.AppendEntity(line);
  131.                 tr.AddNewlyCreatedDBObject(line, true);
  132.  
  133.                 tr.Commit();
  134.             }
  135.             Application.DocumentManager.MdiActiveDocument.Editor.UpdateScreen();
  136.         }
  137.  
  138.         public ObservableCollection<LineWeight> LineWeights { get; set; }
  139.  
  140.         public ViewModel()
  141.         {
  142.             LineWeights = new ObservableCollection<LineWeight>();
  143.             foreach (LineWeight lineWeight in Enum.GetValues(typeof(LineWeight)))
  144.             {
  145.                 LineWeights.Add(lineWeight);
  146.             }
  147.         }
  148.  
  149.         [NotifyPropertyChangedInvocator]
  150.         protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  151.         {
  152.             var handler = PropertyChanged;
  153.             if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
  154.         }
  155.     }
  156.  
  157. }
  158.  


The Dialog
Code - C#: [Select]
  1. <Window x:Class="LineExample.AddLineDialog"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6.         xmlns:lineExample="clr-namespace:LineExample"
  7.         SizeToContent="Height" Width="300"
  8.         mc:Ignorable="d" d:DataContext="{d:DesignInstance lineExample:ViewModel}">
  9.     <Window.DataContext>
  10.         <lineExample:ViewModel/>
  11.     </Window.DataContext>
  12.     <Grid Margin="10">
  13.         <Grid.RowDefinitions>
  14.             <RowDefinition Height="Auto"/>
  15.             <RowDefinition Height="Auto"/>
  16.             <RowDefinition Height="Auto"/>
  17.             <RowDefinition Height="Auto"/>
  18.             <RowDefinition Height="Auto"/>
  19.             <RowDefinition Height="Auto"/>
  20.             <RowDefinition Height="Auto"/>
  21.             <RowDefinition Height="35"/>
  22.         </Grid.RowDefinitions>
  23.         <TextBlock Text="Start Point:" Grid.Row="0" VerticalAlignment="Center"/>
  24.         <Grid Grid.Row="1">
  25.             <Grid.ColumnDefinitions>
  26.                 <ColumnDefinition Width="Auto"/>
  27.                 <ColumnDefinition Width="*"/>
  28.                 <ColumnDefinition Width="Auto"/>
  29.                 <ColumnDefinition Width="*"/>
  30.                 <ColumnDefinition Width="Auto"/>
  31.                 <ColumnDefinition Width="*"/>
  32.             </Grid.ColumnDefinitions>
  33.             <TextBlock Grid.Column="0" Text="X:" VerticalAlignment="Center" />
  34.             <TextBox Text="{Binding Path=StartPointX, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" Margin="5"/>
  35.             <TextBlock Grid.Column="2" Text="Y:" VerticalAlignment="Center" />
  36.             <TextBox Text="{Binding Path=StartPointY, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="3" Margin="5"/>
  37.             <TextBlock Grid.Column="4" Text="Z:" VerticalAlignment="Center" />
  38.             <TextBox Text="{Binding Path=StartPointZ, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="5" Margin="5"/>
  39.         </Grid>
  40.         <TextBlock Text="End Point:" Grid.Row="2" VerticalAlignment="Center"/>
  41.         <Grid Grid.Row="3">
  42.             <Grid.ColumnDefinitions>
  43.                 <ColumnDefinition Width="Auto"/>
  44.                 <ColumnDefinition Width="*"/>
  45.                 <ColumnDefinition Width="Auto"/>
  46.                 <ColumnDefinition Width="*"/>
  47.                 <ColumnDefinition Width="Auto"/>
  48.                 <ColumnDefinition Width="*"/>
  49.             </Grid.ColumnDefinitions>
  50.             <TextBlock Grid.Column="0" Text="X:" VerticalAlignment="Center" />
  51.             <TextBox Text="{Binding Path=EndPointX, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" Margin="5"/>
  52.             <TextBlock Grid.Column="2" Text="Y:" VerticalAlignment="Center" />
  53.             <TextBox Text="{Binding Path=EndPointY, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="3" Margin="5"/>
  54.             <TextBlock Grid.Column="4" Text="Z:" VerticalAlignment="Center" />
  55.             <TextBox Text="{Binding Path=EndPointZ, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="5" Margin="5"/>
  56.         </Grid>        
  57.         <TextBlock Text="Line Weight:" Grid.Row="4" VerticalAlignment="Center"/>
  58.         <ComboBox ItemsSource="{Binding Path=LineWeights}"
  59.                   SelectedItem="{Binding Path=LineWeight, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
  60.                   IsSynchronizedWithCurrentItem="True" Grid.Row="5" Margin="5"/>
  61.         <Button Content="Create Line" Margin="5" Grid.Row="6" Command="{Binding Path=CreateLineCommand}"/>
  62.         <StackPanel Grid.Row="7" Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Center">
  63.             <Button Content="CANCEL" Width="75" Margin="5" IsCancel="True"/>
  64.             <Button Content="OK" Width="75" Margin="5" Click="DialogOkBtnClick"/>
  65.         </StackPanel>
  66.     </Grid>
  67. </Window>
  68.  

The Dialog Code Behind:
Code - C#: [Select]
  1. using System.Windows;
  2.  
  3. namespace LineExample
  4. {
  5.     public partial class AddLineDialog
  6.     {
  7.         public AddLineDialog()
  8.         {
  9.             InitializeComponent();
  10.         }
  11.  
  12.         private void DialogOkBtnClick(object sender, RoutedEventArgs e)
  13.         {
  14.             DialogResult = true;
  15.         }
  16.     }
  17. }
  18.  

The AutoCad Command
Code - C#: [Select]
  1. using Autodesk.AutoCAD.ApplicationServices.Core;
  2. using Autodesk.AutoCAD.Runtime;
  3.  
  4. namespace LineExample
  5. {
  6.     public class Commands
  7.     {
  8.         [CommandMethod("ADDLINE")]
  9.         public static void AddLine()
  10.         {
  11.             var dialog = new AddLineDialog();
  12.             Application.ShowModalWindow(dialog);
  13.         }
  14.     }
  15. }
  16.  

The RelayCommand to wire up Commands via MVVM. You can Google this and find 100+ examples. This one is just the one I use.
Code - C#: [Select]
  1. using System;
  2. using System.Diagnostics;
  3. using System.Windows.Input;
  4.  
  5. namespace LineExample
  6. {
  7.     public class RelayCommand : ICommand
  8.     {
  9.         private readonly Action<object> _execute;
  10.         private readonly Predicate<object> _canExecute;
  11.  
  12.         public RelayCommand(Action<object> execute)
  13.             : this(execute, null)
  14.         {
  15.  
  16.         }
  17.  
  18.         public RelayCommand(Action<object> execute, Predicate<object> canExecute)
  19.         {
  20.             if (execute == null)
  21.                 throw new ArgumentNullException("execute");
  22.  
  23.             _execute = execute;
  24.             _canExecute = canExecute;
  25.         }
  26.  
  27.         [DebuggerStepThrough]
  28.         public bool CanExecute(object parameter)
  29.         {
  30.             return _canExecute == null || _canExecute(parameter);
  31.         }
  32.  
  33.         public void Execute(object parameter)
  34.         {
  35.             _execute(parameter);
  36.         }
  37.  
  38.         public event EventHandler CanExecuteChanged
  39.         {
  40.             add { CommandManager.RequerySuggested += value; }
  41.             remove { CommandManager.RequerySuggested -= value; }
  42.         }
  43.     }
  44. }
  45.  


Can't post a screen shot since I can't get to photobucket from my office PC.
Revit 2019, AMEP 2019 64bit Win 10

Coder

  • Swamp Rat
  • Posts: 827
Re: Link a dialog with class file
« Reply #2 on: April 16, 2015, 12:16:21 PM »
Thank you so much for that hard work , I can't thank you enough  :-)

I have no idea how to load all these codes  :-o feeling lost more than before . I am sorry  :-(

Many thanks

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Link a dialog with class file
« Reply #3 on: April 16, 2015, 12:57:29 PM »
Attached is the entire Solution so you can open it.  If you want to run it then don't forget to fix your Autocad references and under the Project properties path the startup file to your Autocad installation.

Keep in mind this is a horrible example of MVVM.  Everything should be divided up into folders, interface classes and base classes are missing. With a little research on MVVM it will all make sense.
« Last Edit: April 16, 2015, 01:01:28 PM by MexicanCustard »
Revit 2019, AMEP 2019 64bit Win 10

Coder

  • Swamp Rat
  • Posts: 827
Re: Link a dialog with class file
« Reply #4 on: April 16, 2015, 01:14:44 PM »
Waw that is really fantastic , and the dialog looks different than the dialogs I have used to see  :-)

Really nice , thank you so much .

CADbloke

  • Bull Frog
  • Posts: 342
  • Crash Test Dummy
Re: Link a dialog with class file
« Reply #5 on: April 16, 2015, 06:53:36 PM »
This is a horrible bastard thing to do to WPF but use it as a learning tool, it's a method I wrote to turn a Windows Form into a WPF window. It will create a FrankenWindow - you shouldn't even remotely consider it a conversion process but I think it would be a great way to compare how the UI is built in the two technologies. ok, disclaimers out of the way: https://gist.github.com/CADbloke/0a3121af11c1d46f34c9

note to self: post my collection of links I gathered when I binged on WPF MVVM learning a few months ago (It's not on this computer).

If you're making WPF things, I recommend using Blend:  (the VS2013 Community Edition has Blend ) - especially if you're trying to make sense of the FrankenWPF that gist I linked to generates.
« Last Edit: April 16, 2015, 06:58:52 PM by CADbloke »

CADbloke

  • Bull Frog
  • Posts: 342
  • Crash Test Dummy
Re: Link a dialog with class file
« Reply #6 on: April 28, 2015, 06:02:54 AM »

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Link a dialog with class file
« Reply #7 on: April 28, 2015, 07:46:18 AM »
I posted those WPF links. http://www.theswamp.org/index.php?topic=32381.msg544542#msg544542

As far as the speed of EF,  it greatly depends on the size of the SQL statement you produce.  Example, by breaking CRUD operations up into smaller chunks you can drastically increase performance.  So if you're adding the contents of 240,000 attributes to a table at one time you can increase performance by writing 10,000 records 24 times.  You probably already know this since you've been reading Julie's books but I just thought it worth mentioning to those just getting into EF.
Revit 2019, AMEP 2019 64bit Win 10

CADbloke

  • Bull Frog
  • Posts: 342
  • Crash Test Dummy
Re: Link a dialog with class file
« Reply #8 on: April 28, 2015, 08:53:11 AM »
As far as the speed of EF,  it greatly depends on the size of the SQL statement you produce.  Example, by breaking CRUD operations up into smaller chunks you can drastically increase performance.  So if you're adding the contents of 240,000 attributes to a table at one time you can increase performance by writing 10,000 records 24 times.  You probably already know this since you've been reading Julie's books but I just thought it worth mentioning to those just getting into EF.

Yeah, it's not an exact science. http://stackoverflow.com/questions/5940225/fastest-way-of-inserting-in-entity-framework has a examples of it, as does http://weblog.west-wind.com/posts/2013/Dec/22/Entity-Framework-and-slow-bulk-INSERTs

I tried writing everything to SQL at the end on in one transaction, it was actually a little quicker but acad.exe's RAM usage went from 350kB to around 800kB when the Entities gained weight, and then to around 6 GB as EF wrote it out to SQL, even with context.Configuration.AutoDetectChangesEnabled = false;. To it's credit, acad.exe hasn't crashed (except when I crash it). Writing to SQL one drawing at a time keeps the RAM usage around 600kB. I don't really care since my Dev laptop has 32GB but it will have to run elsewhere some day. It's still an order of magnitude faster than the VB6 Access & COM-based program that a lot of people are using presently.

It's interesting to see what EF is generating when you watch it all go by on SQL Profiler. You learn pretty quickly that, for all it's abstraction and persistence-ignorance rhetoric, you can't ignore the SQL.

https://efbulkinsert.codeplex.com/ looked promising but it doesn't do any sort of hierarchy, only one flat level of entities. It's basically SqlBulkCopy wrapped up all nice and pretty.

I haven't had time to thoroughly digest http://www.sysexpand.com/?path=howto/bulk-insert but it looks interesting.
« Last Edit: April 28, 2015, 09:15:43 AM by CADbloke »