Author Topic: .NET newbie question  (Read 8848 times)

0 Members and 1 Guest are viewing this topic.

Nathan Taylor

  • Guest
Re: .NET newbie question
« Reply #15 on: March 04, 2008, 11:28:33 PM »
Quote
I haven't done any vb.net but I can read it fairly well as I see it as the same structure as my C# coding using vba words

I like that observation as it helps explain the difference between the API's. The similarity is where the .NET API is being used. If the the ActiveX API was being used then the VB.NET code would look very close to VBA code and the C# code would look like VBA code with C# words.

The reason I suggest people moving from VBA use VB.NET is because the difference between the languages is minor. So instead of spending 4 hours learning a new Select Case they can spend it learning how to use transactions.

There seems to be a need for a place that explains the different API technologies and their benefits/disadvantages and how they are used.

Unfortunately there are people using the ActiveX API with .NET languages. In some case they have a legitimate reason. In some cases they are coming in from the outside and are ignorant of the APIs availalble. In some cases they are moving from VBA, learn the new language and continue programming just as they did with VBA so don't really gain much.

One of the arguments the C#ers have used which I think has relevance is by using C# they can make a clean break from VBA. Unfortunately I have seen ex VBAers using C# to program AutoCAD in exactly the same way as they did in VBA which has really made me cringe.

The thing I like about the .NET API users is that at the end of the day no matter what our disagreements are about languages we can see past it and help each other.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8659
  • AKA Daniel
Re: .NET newbie question
« Reply #16 on: March 05, 2008, 01:18:53 AM »
The thing I like about the .NET API users is that at the end of the day no matter what our disagreements are about languages we can see past it and help each other.

Well said

It doesn’t really matter what language one chooses to write with, but I would recommend people who choose VB
at least learn enough of the C style languages to be able to read the ObjectARX docs and C# users learn enough
about VB/A to read the ActiveX and VBA Developer's Guide. 

MaksimS

  • Guest
Re: .NET newbie question
« Reply #17 on: March 05, 2008, 05:54:58 AM »
Just my 2 cents:

- Consider switching to VB.NET if you have extensive VB/VBA background
- In .NET, VB.NET and C# (including any other .NET language) have equal potentials
- Compared to ActiveX technology, .NET will get you a free ticket out of DLL hell
- .NET controls/components are way more "reusable" than ActiveX controls for that matter
- Kickstart in .NET will make you learn and understand OOP better than VB/VBA
- VS.NET is much more RAD-ready than any other IDE
- You may continue to use existing custom developed ActiveX contols in .NET, and translate them to .NET gradually
- When talking about managed ObjectARX, it will give you much more control over underlying Autodesk core than TLBs

Managed ObjectARX is simply a wrapper (with bonus) over native ObjectARX - once you understand how things work, you will notice that Autodesk's ActiveX controls introduce more abstraction and provide less control over ACAD's core. Remember, what you currently see through TLB sunglasses is _not_ how things work under the hood.

Regards,
Maksim Sestic


Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: .NET newbie question
« Reply #18 on: March 07, 2008, 04:22:47 AM »
<snip>   A good example would be offsetting a line. 
<snip>
How would you do this with .NET?  <snip>
 

Was this a rhetorical question ?

The Autodesk.AutoCAD.Geometry.OffsetCurve3d looks like a winner ...

I'll have some time tomorrow, if you don't yet have a solution. ... an extension method to add to a library shouldn't be  too difficult to knock together :-)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8659
  • AKA Daniel
Re: .NET newbie question
« Reply #19 on: March 07, 2008, 04:32:20 AM »
<snip>   A good example would be offsetting a line. 
<snip>
How would you do this with .NET?  <snip>
 

Was this a rhetorical question ?

The Autodesk.AutoCAD.Geometry.OffsetCurve3d looks like a winner ...

I'll have some time tomorrow, if you don't yet have a solution. ... an extension method to add to a library shouldn't be  too difficult to knock together :-)

Also have a look at

Code: [Select]
DBObjectCollection Curve.GetOffsetCurves(double offsetDist);

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: .NET newbie question
« Reply #20 on: March 07, 2008, 04:33:16 AM »
or
Autodesk.AutoCAD.DatabaseServices.Curve
public virtual DBObjectCollection GetOffsetCurves(double offsetDist);
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: .NET newbie question
« Reply #21 on: March 07, 2008, 04:34:31 AM »
ahhhh. beaten by a smidgen :-)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8659
  • AKA Daniel
Re: .NET newbie question
« Reply #22 on: March 07, 2008, 04:39:59 AM »
I think this one has a bug in it when it comes to polylines, you have to do something like

Code: [Select]
   if (curve.GetType().Name == "Polyline")
      {
        distance = -distance;
      }

Could have been fixed though..?

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: .NET newbie question
« Reply #23 on: March 07, 2008, 04:43:35 AM »
I'll let you know tomorrow Dan .. I recall reading something similar ..

added:
unless some enterprising bod tackles it first ;-) 

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8659
  • AKA Daniel
Re: .NET newbie question
« Reply #24 on: March 08, 2008, 07:50:45 AM »
I think this was one my first kludges with C#, there is an offset in here somewhere.  :lol:

Code: [Select]
using System;
using System.Collections.Generic;
using System.Text;
//
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using Trans = Autodesk.AutoCAD.DatabaseServices;
namespace CrapDraw.Dualoffset
{
  public static class Dualoffset
  {
    [CommandMethod("dualoffset")]
    public static void dualoffset()
    {
      Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
      try
      {
        double offsetdist;
        offsetdist = (double)AcadApp.GetSystemVariable("OFFSETDIST");
        offsetdist = GetDouble("\nSpecify offset distance: ", offsetdist);
        if (offsetdist != 0)
        {
          Entity en = GetEntity("\nSelect object to offset: ");
          Offset(en, offsetdist / 2.0);
          Offset(en, -offsetdist / 2.0);
          Erase(en);
          AcadApp.SetSystemVariable("OFFSETDIST", offsetdist);
        }
      }
      catch
      {
        ed.WriteMessage("\n Error");
      }
    }
    private static ObjectIdCollection Offset(Entity ent, double distance)
    {
      ObjectIdCollection objectIdCollection = new ObjectIdCollection();
      Curve curve = ent as Curve;
      if (curve is Polyline)
      {
        distance = -distance;
      }
      DBObjectCollection dbObjectCollection = curve.GetOffsetCurves(distance);
      Database database = HostApplicationServices.WorkingDatabase;
      Trans.TransactionManager manager = database.TransactionManager;
      using (Transaction transaction = manager.StartTransaction())
      {
        BlockTable table = (BlockTable)manager.GetObject
              (database.BlockTableId, OpenMode.ForRead, false);

        BlockTableRecord record = (BlockTableRecord)manager.GetObject
               (table[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false);

        foreach (DBObject obj in dbObjectCollection)
        {
          Entity entity = obj as Entity;
          objectIdCollection.Add(record.AppendEntity(entity));
          manager.AddNewlyCreatedDBObject(entity, true);
        }
        transaction.Commit();
      }
      return objectIdCollection;
    }
    private static Entity GetEntity(string prompt)
    {
      Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
      PromptEntityOptions entopts = new PromptEntityOptions(prompt);
      entopts.Message = prompt;
      entopts.AllowNone = false;
      PromptEntityResult ent = null;
      try
      {
        ent = ed.GetEntity(entopts);
      }
      catch
      {
        ed.WriteMessage("You did not select a valid entity");
      }
      if (ent.Status != PromptStatus.Error)
      {
        ObjectId entid = ent.ObjectId;
        Database database = HostApplicationServices.WorkingDatabase;
        try
        {
          Trans.TransactionManager manager = database.TransactionManager;
          using (Transaction transaction = manager.StartTransaction())
          {
            Entity entity = (Entity)manager.GetObject(entid, OpenMode.ForRead, true);//
            transaction.Commit();
            return entity;
          }
        }
        catch { }
      }
      return null;
    }
    private static void Erase(Entity ent)
    {
      Erase(ent.ObjectId);
    }
    private static void Erase(ObjectId id)
    {
      Trans.TransactionManager manager =
        HostApplicationServices.WorkingDatabase.TransactionManager;

      using (Transaction transaction = manager.StartTransaction())
      {
        ((Entity)manager.GetObject(id, OpenMode.ForWrite, true)).Erase();
        transaction.Commit();
      }
    }
    private static double GetDouble(string prompt, double def)
    {
      Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
      PromptDoubleOptions opt1 = new PromptDoubleOptions(prompt);
      opt1.AllowNone = false;
      opt1.AllowZero = false;
      opt1.DefaultValue = def;
      PromptDoubleResult doubleRes = ed.GetDouble(opt1);
      if (doubleRes.Status == PromptStatus.OK)
      {
        return (double)doubleRes.Value;
      }
      else
      {
        throw new System.NullReferenceException();
      }
    }
  }
}
« Last Edit: March 08, 2008, 08:13:51 AM by Daniel »