Author Topic: .NET MISCELLANEOUS/GENERAL Routines  (Read 23850 times)

0 Members and 1 Guest are viewing this topic.

Dashmonkey

  • Newt
  • Posts: 29
  • (defun sleep nil nil)
Re: .NET MISCELLANEOUS/GENERAL Routines
« Reply #45 on: July 20, 2015, 04:41:53 pm »
Gile, I'm loving your AcadReg class! It's SUPER slick and I learned a lot from looking at it. Thanks for posting :D
I didn't break it, I swear! ...ok, I broke it.

Keith Brown

  • Swamp Rat
  • Posts: 585
Re: .NET MISCELLANEOUS/GENERAL Routines
« Reply #46 on: August 12, 2015, 02:42:18 pm »
Hi Gile,

In some of your methods in the Registry Classes I had to change your OpenSubKey calls to open with ReadWrite status.  For instance in the AddPath method of the ProfileKey class I changed

Code - C#: [Select]
  1. using (var rkey= Registry.CurrentUser.OpenSubKey(key))

to

Code - C#: [Select]
  1. using (var rkey= Registry.CurrentUser.OpenSubKey(key, true))

I had to do this in several places where the value of the key was being modified.  Not sure if that was because of my permissions or something else.  I think that you are also missing the servicePack private string variable in the ProductKey class.  Other than that it worked great for me when installing a dll.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

FriendFromArea51

  • Swamp Rat
  • Posts: 625
  • Mars sucks!
Re: .NET MISCELLANEOUS/GENERAL Routines
« Reply #47 on: August 12, 2015, 09:28:38 pm »
Toggles extension line for aligned dimension...doesn't account for exact middle pick but found it's never been a problem:

Code - C#: [Select]
  1.        [CommandMethod("TOGE")]
  2.        public static void RemoveAddExtensions()
  3.        {
  4.            CivilDocument CivDoc = Autodesk.Civil.ApplicationServices.CivilApplication.ActiveDocument;
  5.            Document AcadDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  6.            Editor MyEditor = Application.DocumentManager.MdiActiveDocument.Editor;
  7.            Database MyDatabase = HostApplicationServices.WorkingDatabase;
  8.  
  9.            using (DocumentLock acLckDoc = AcadDoc.LockDocument())
  10.            {
  11.                using (Transaction MyTrans = MyDatabase.TransactionManager.StartTransaction())
  12.                {
  13.                    try
  14.                    {
  15.                        ObjectId mydimoid = promptfordim("\nSelect Dimension: ");
  16.                        AlignedDimension mydim = (AlignedDimension)MyTrans.GetObject(mydimoid, OpenMode.ForRead);
  17.                        Point3d mypoint = GetMyPoint("\nSelect Point Near Extension: ");
  18.                        mydim.UpgradeOpen();
  19.  
  20.                        if ((mypoint.DistanceTo(mydim.XLine1Point)) < (mypoint.DistanceTo(mydim.XLine2Point)))
  21.                        {
  22.                            if (mydim.Dimse1 == true)
  23.                            {
  24.                                mydim.Dimse1 = false;
  25.                            }
  26.                            else
  27.                            {
  28.                                mydim.Dimse1 = true;
  29.                            }
  30.                        }
  31.                        else
  32.                        {
  33.                            if (mydim.Dimse2 == true)
  34.                            {
  35.                                mydim.Dimse2 = false;
  36.                            }
  37.                            else
  38.                            {
  39.                                mydim.Dimse2 = true;
  40.                            }
  41.                        }
  42.                        mydim.DowngradeOpen();
  43.                    }
  44.                    catch (System.Exception ex)
  45.                    {
  46.                        MyEditor.WriteMessage("Error: ==>\n{0}\nTrace: ==>\n{1}", ex.Message, ex.StackTrace);
  47.                    }
  48.                    MyTrans.Commit();
  49.                }
  50.            }
  51.        }[code]

gile

  • Water Moccasin
  • Posts: 2089
  • Marseille, France
Re: .NET MISCELLANEOUS/GENERAL Routines
« Reply #48 on: August 14, 2015, 04:09:21 am »
Hi Gile,

In some of your methods in the Registry Classes I had to change your OpenSubKey calls to open with ReadWrite status.  For instance in the AddPath method of the ProfileKey class I changed

Code - C#: [Select]
  1. using (var rkey= Registry.CurrentUser.OpenSubKey(key))

to

Code - C#: [Select]
  1. using (var rkey= Registry.CurrentUser.OpenSubKey(key, true))

I had to do this in several places where the value of the key was being modified.  Not sure if that was because of my permissions or something else.  I think that you are also missing the servicePack private string variable in the ProductKey class.  Other than that it worked great for me when installing a dll.
Many thanks for reporting Keith.
I corrected the posted code.
Speaking English as a French Frog

RICVBA

  • Newt
  • Posts: 51
Re: .NET MISCELLANEOUS/GENERAL Routines
« Reply #49 on: August 23, 2015, 03:19:06 am »
Nice idea kaefer (as usual).
If I have some time, I'd add some WriteRange() method to the ExcelWriter class.

Hi Everybody
I'm a rather discontinuos learner of C# and NET, with the main purpose of Autocad Automation and its Excel connection
These days, I peeped in this thread to try and learn "true life" coding and state-of-the-art "how to"'s,

While learning a lot (and realizing there's a very lot more to - probably too much!), I assumed it could be a useful exercise to make some add-on and see what I actually grasped of it

so I'm attacching a LateBinding solution "ExcelReader" class with minor modifications to let you deal with Excel data written in and read from ranges not beginning at cell (1,1)
In detail, I modifed the following ExcelReader methods:
- ExcelReader main constructor
- SetActiveSheet()
- Peak()
- NewLine()
- Read() (the main method)
- ReadLine() (the main method)
- ReadRange() (both methods)
- RangeToDataTable() (both methods)
all modifications are clearly marked and I also mantained (commented) the corresponding original code lines

to test it I slightly modified Gile's using example as follows:

Using Example:
Code: [Select]
class Program
    {
        static void Main(string[] args)
        {
            using (ExcelWriter xlw = new ExcelWriter(@"C:\Users\Riccardo\Documents\Cartel2.xlsx"))
            {
                xlw.Clear();
                xlw.WriteLine(new object[3] { "foo", "bar", "baz" }, 3, 2); //<------ modified by RICVBA to have a usedrange not beginning at cells(1,1)
                xlw.WriteLine(new object[3] { 1, 2, 3 }, 4, 3);             //<------ modified by RICVBA to have a usedrange not beginning at cells(1,1)
                xlw.Save();
            }

            using (ExcelReader xlr = new ExcelReader(@"C:\Users\Riccardo\Documents\Cartel2.xlsx"))
            {
                while (!xlr.StreamEnded)
                {
                    foreach (object obj in xlr.ReadLine())
                    {
                        Console.Write("{0} ", obj);
                    }
                    Console.WriteLine();
                }

                //------------------------------------------
                //added by RICVBA to test some methods after ExcelReader class modifications
                wl(xlr.ReadToEnd()); // should work as it was
                wl(xlr.ReadRange(2, 1)); // modified
                //------------------------------------------

            }

            Console.ReadLine();
        }

        //------------------------------------------
        //added by RICVBA
        static void wl(object[,] range)
        {
            for (int i = 0; i < range.GetLength(0); i++)
            {
                for (int j = 0; j < range.GetLength(1); j++)
                {
                    Console.Write("{0} ", range[i, j]);
                }
                Console.WriteLine();
            }
        }
        //------------------------------------------


as per my tests, it worked.
if confirmed, same modifications for Dynamic solution's same class should be straightforward
If anybody interested in giving feedbacks, please do.

thank you   

gile

  • Water Moccasin
  • Posts: 2089
  • Marseille, France
Re: .NET MISCELLANEOUS/GENERAL Routines
« Reply #50 on: October 01, 2015, 02:36:38 pm »
Hi,

I updated the code of AutocadRegistryServices to handle the reversed values of PatchTitle and ProductName in the registry with AutoCAD 2016 SP1.

Speaking English as a French Frog

gile

  • Water Moccasin
  • Posts: 2089
  • Marseille, France
Re: .NET MISCELLANEOUS/GENERAL Routines
« Reply #51 on: March 10, 2017, 05:09:40 pm »
Hi,

Some extension methods to perform different kinds of zoom.

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 System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8.  
  9. namespace Autodesk.AutoCAD.EditorInput
  10. {
  11.    public static class ExtensionMethods
  12.    {
  13.        public static Matrix3d EyeToWorld(this ViewTableRecord view)
  14.        {
  15.            if (view == null)
  16.                throw new ArgumentNullException("view");
  17.  
  18.            return
  19.                Matrix3d.Rotation(-view.ViewTwist, view.ViewDirection, view.Target) *
  20.                Matrix3d.Displacement(view.Target - Point3d.Origin) *
  21.                Matrix3d.PlaneToWorld(view.ViewDirection);
  22.        }
  23.  
  24.        public static Matrix3d WorldToEye(this ViewTableRecord view)
  25.        {
  26.            return view.EyeToWorld().Inverse();
  27.        }
  28.  
  29.        public static void Zoom(this Editor ed, Extents3d ext)
  30.        {
  31.            if (ed == null)
  32.                throw new ArgumentNullException("ed");
  33.  
  34.            using (ViewTableRecord view = ed.GetCurrentView())
  35.            {
  36.                ext.TransformBy(view.WorldToEye());
  37.                view.Width = ext.MaxPoint.X - ext.MinPoint.X;
  38.                view.Height = ext.MaxPoint.Y - ext.MinPoint.Y;
  39.                view.CenterPoint = new Point2d(
  40.                    (ext.MaxPoint.X + ext.MinPoint.X) / 2.0,
  41.                    (ext.MaxPoint.Y + ext.MinPoint.Y) / 2.0);
  42.                ed.SetCurrentView(view);
  43.            }
  44.        }
  45.  
  46.        public static void ZoomExtents(this Editor ed)
  47.        {
  48.            if (ed == null)
  49.                throw new ArgumentNullException("ed");
  50.  
  51.            Database db = ed.Document.Database;
  52.            db.UpdateExt(false);
  53.            Extents3d ext = (short)Application.GetSystemVariable("cvport") == 1 ?
  54.                new Extents3d(db.Pextmin, db.Pextmax) :
  55.                new Extents3d(db.Extmin, db.Extmax);
  56.            ed.Zoom(ext);
  57.        }
  58.  
  59.        public static void ZoomWindow(this Editor ed, Point3d p1, Point3d p2)
  60.        {
  61.            using (Line line = new Line(p1, p2))
  62.            {
  63.                ed.Zoom(line.GeometricExtents);
  64.            }
  65.        }
  66.  
  67.        public static void ZoomScale(this Editor ed, double scale)
  68.        {
  69.            if (ed == null)
  70.                throw new ArgumentNullException("ed");
  71.  
  72.            using (ViewTableRecord view = ed.GetCurrentView())
  73.            {
  74.                view.Width /= scale;
  75.                view.Height /= scale;
  76.                ed.SetCurrentView(view);
  77.            }
  78.        }
  79.  
  80.        public static void ZoomObjects(this Editor ed, IEnumerable<ObjectId> ids)
  81.        {
  82.            if (ed == null)
  83.                throw new ArgumentNullException("ed");
  84.  
  85.            using (Transaction tr = ed.Document.TransactionManager.StartTransaction())
  86.            {
  87.                Extents3d ext = ids
  88.                    .Where(id => id.ObjectClass.IsDerivedFrom(RXObject.GetClass(typeof(Entity))))
  89.                    .Select(id => ((Entity)tr.GetObject(id, OpenMode.ForRead)).GeometricExtents)
  90.                    .Aggregate((e1, e2) => { e1.AddExtents(e2); return e1; });
  91.                ed.Zoom(ext);
  92.                tr.Commit();
  93.            }
  94.        }
  95.  
  96.        public static void ZoomCenter(this Editor ed, Point3d center, double scale = 1.0)
  97.        {
  98.            if (ed == null)
  99.                throw new ArgumentNullException("ed");
  100.  
  101.            using (ViewTableRecord view = ed.GetCurrentView())
  102.            {
  103.                center = center.TransformBy(view.WorldToEye());
  104.                view.Height /= scale;
  105.                view.Width /= scale;
  106.                view.CenterPoint = new Point2d(center.X, center.Y);
  107.                ed.SetCurrentView(view);
  108.            }
  109.        }
  110.    }
  111. }
  112.  
« Last Edit: July 01, 2017, 02:00:10 am by gile »
Speaking English as a French Frog

Keith Brown

  • Swamp Rat
  • Posts: 585
Re: .NET MISCELLANEOUS/GENERAL Routines
« Reply #52 on: March 16, 2017, 02:00:12 pm »
Hi,
Some extension methods to perform different kinds of zoom.


Hi Gile,


Nice code but you might want to wrap your max minus min functions in an absolute value call before passing to the width and height of the view.  Autocad seems to handle it fine with out the absolute function call but Bricscad will throw an exception when both points are in negative territory.  Autocad must be automatically taking the absolute value of the width and height where Teigha (bricscad) is not.


Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013