TheSwamp

Code Red => .NET => Topic started by: Bobby C. Jones on January 03, 2006, 02:15:47 PM

Title: AU 2005 - Example Code Project
Post by: Bobby C. Jones on January 03, 2006, 02:15:47 PM
Per request, attached are the VS2005 project files for my AU2005 code samples.  I *may* have an older VS2003 version of this project lying around if anyone is interested.  Just let me know.
Title: Re: AU 2005 - Example Code Project
Post by: LE on January 03, 2006, 03:48:44 PM
Per request, attached are the VS2005 project files for my AU2005 code samples.  I *may* have an older VS2003 version of this project lying around if anyone is interested.  Just let me know.

Is possible to run this samples on SharpDevelop ? ... [I have AutoCAD 2005]

Thanks.
Title: Re: AU 2005 - Example Code Project
Post by: MickD on January 03, 2006, 04:25:08 PM
Yes you can Luis, you may have to change the 'using' statements slightly as it seems SharpDev reads the ref'd assemblies a little differently, this may have changed in later releases though.
Also, if the sample was built for 2006 it probably won't compile for 2005 as they changed quite a few of the wrappers (mainly for the editor and UI) and made them more complete for 2006.

If you have VS2002 you should have no trouble, this is what I use at the moment with no prob's so far.
hth
Title: Re: AU 2005 - Example Code Project
Post by: LE on January 03, 2006, 04:30:48 PM
Yes you can Luis, you may have to change the 'using' statements slightly as it seems SharpDev reads the ref'd assemblies a little differently, this may have changed in later releases though.
Also, if the sample was built for 2006 it probably won't compile for 2005 as they changed quite a few of the wrappers (mainly for the editor and UI) and made them more complete for 2006.

If you have VS2002 you should have no trouble, this is what I use at the moment with no prob's so far.
hth

 :-(

I see, I just want to play for now.... nope it will allow only 2003 solutions... I have VS2002 but it is the "standard" version.....  does not include C#... OK.. I will download the 2005 express.... for now [will that work?]

Thanks.
Title: Re: AU 2005 - Example Code Project
Post by: MickD on January 03, 2006, 04:47:40 PM
Before you do, create a new project, add your references and add just the .cs files into your project (or copy and paste the code from a std text editor into new files). It's only the version of vs that's givving you the trouble ;)
Title: Re: AU 2005 - Example Code Project
Post by: LE on January 03, 2006, 05:10:16 PM
Before you do, create a new project, add your references and add just the .cs files into your project (or copy

Let me explain, what I did before:

1. I started a Class Library
2. Added the references: From the .NET assembly browser and use: "ACDBMGD.DLL" and "ACMGD.DLL"
3. In the HelloWorld.cs - I added:

Code: [Select]
using System;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace HelloWorld
{
/// <summary>
/// Description of HelloWorld.
/// </summary>
public class HelloWorld
{
[Autodesk.AutoCAD.Runtime.CommandMethod("HELLO")]
public void HelloCommand()
{
acadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("Hello World FROM C#!");
acadApp.UpdateScreen();
 
}

}
}

I hit the Build option and get:
'Autodesk.AutoCAD.ApplicationServices.Document' does not contain a definition for 'Editor'

Title: Re: AU 2005 - Example Code Project
Post by: MickD on January 03, 2006, 05:34:12 PM
Luis, try replacing

acadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("Hello World FROM C#!");

with -

CommandLinePrompts.Message("Hello World FROM C#!");//using the Autodesk.AutoCAD.Application namespace
Title: Re: AU 2005 - Example Code Project
Post by: LE on January 03, 2006, 05:42:06 PM
Luis, try replacing

acadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("Hello World FROM C#!");

with -

CommandLinePrompts.Message("Hello World FROM C#!");//using the Autodesk.AutoCAD.Application namespace

 :-o
Aaaaaaaaaaa..... that was it.... that's good!!!, where can I read about those functions or calls? - I see that I do not have to close nothing?... all is managed code like in LISP ?.....
Title: Re: AU 2005 - Example Code Project
Post by: MickD on January 03, 2006, 05:49:03 PM
You'll probably have to look at the doc's that came with your arx sdk and find suitable replacements for things that won't work. Just holler if you have trouble finding suitable classes.
Quite a few things changed from 2005 to 2006 including some of the geometry classes (matrices for eg) so this will happen quite a bit!

Once you start opening objects and bt's etc. you will need to close them unless you are using transactions which will take care of this for you, I'm not sure if this is the case with arx and tranactions though(?).
Title: Re: AU 2005 - Example Code Project
Post by: Kerry on January 03, 2006, 05:52:20 PM
Luis, try replacing

acadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("Hello World FROM C#!");

with -

CommandLinePrompts.Message("Hello World FROM C#!");//using the Autodesk.AutoCAD.Application namespace

I was trying to find some old code .. I couldn't remember that .. DUH !
Title: Re: AU 2005 - Example Code Project
Post by: MickD on January 03, 2006, 05:54:16 PM
Here's some old code I used for a template you may find useful.
Title: Re: AU 2005 - Example Code Project
Post by: LE on January 03, 2006, 06:02:38 PM
Once you start opening objects and bt's etc. you will need to close them unless you are using transactions which will take care of this for you, I'm not sure if this is the case with arx and tranactions though(?).

OK, I think not sure.... it is the same as in ObjectARX

Code: [Select]
long i, length;
ads_name ename;
AcDbObjectId entId;
acedSSLength(m_sset, &length);

// Traverse selected entities
acdbTransactionManager->startTransaction();

for (i = 0; i < length; i++) {
// obtener el objectId de cada entidad
acedSSName(m_sset, i, ename);
acdbGetObjectId(entId, ename);
// abrir la entidad y borrarla
// es recomendable inicializar el pointer
// y darle el valor de 0 o NULL que es lo mismo
AcDbEntity* pEnt = NULL;
if (acdbTransactionManager->getObject((AcDbObject*&)pEnt,entId,AcDb::kForWrite) == Acad::eOk) {
pEnt->erase();
}
}

acdbTransactionManager->endTransaction();

Are Smart Pointers available on C# ?
Title: Re: AU 2005 - Example Code Project
Post by: Kerry on January 03, 2006, 06:07:30 PM
Luis, Have alook at this by Glenn
http://www.theswamp.org/forum/index.php?topic=8197.msg105289#msg105289

Does almost the same thing


Well, NOT the same, but you'll get the idea :-)
Title: Re: AU 2005 - Example Code Project
Post by: LE on January 03, 2006, 06:18:22 PM
Luis, Have alook at this by Glenn
http://www.theswamp.org/forum/index.php?topic=8197.msg105289#msg105289

Does almost the same thing


Well, NOT the same, but you'll get the idea :-)

Start to get confused............  :lol:

I don't see in there any place to call the endTransaction() ... It is because if Managed Code no?

Well.... at least I have an idea now [I think]
Title: Re: AU 2005 - Example Code Project
Post by: Kerry on January 03, 2006, 06:23:53 PM
The EndTransaction is automagic BECAUSE the code operates inside this :

using (Transaction tr = ...
{


   
}

     
Title: Re: AU 2005 - Example Code Project
Post by: MickD on January 03, 2006, 06:27:27 PM
This is another way to do it without using 'using'
Code: [Select]
public void SomeFoo()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Transaction tr = db.TransactionManager.StartTransaction();
try
{
//do stuff here!
tr.Commit();
}
catch
{
ed.WriteMessage("KaBoom!..");
}
finally
{
tr.Dispose();
}
}

as with most things there are a few ways to do them :-)
Title: Re: AU 2005 - Example Code Project
Post by: Glenn R on January 03, 2006, 07:23:34 PM
A 'using' statement is really shorthand for a 'try/finally' construct.

I tend to use the using if it's one thing - if I have more I tend to use try/catch/finally to avoid all the nesting of multiple 'using' statement.

Cheers,
Glenn.
Title: Re: AU 2005 - Example Code Project
Post by: Bobby C. Jones on January 05, 2006, 12:16:08 PM
I tend to use the using if it's one thing - if I have more I tend to use try/catch/finally to avoid all the nesting of multiple 'using' statement.
Hey Glenn,
You can put more than one object declaration in the using statement to avoid nesting.

Code: [Select]
using (DisposableObject1 do1 = new DisposableObject1(),
       DisposableOjbect2 do2 = new DisposableOjbect2())
{
    .....do your thing.....
}

I have also seen this syntax:
Code: [Select]
using (DisposableObject1 do1 = new DisposableObject1())
using (DisposableObject2 do2 = new DisposableObject2())
{
    .....do your thing.....
}
but it doesn't look valid according to the 2.0 docs.  Maybe its OK in an earlier version?  Or more likely, I'm just off my rocker :-)
Title: Re: AU 2005 - Example Code Project
Post by: Glenn R on January 05, 2006, 06:20:50 PM
Heh...I had forgotten about that. Thanks for the reminder.

I still prefer the try/finally construct with multiples, as I tend to declare things as close to possible to when i need them in code...
another habit from C++.

Cheers,
Glenn.
Title: Re: AU 2005 - Example Code Project
Post by: Bobby C. Jones on January 06, 2006, 09:27:28 AM
I still prefer the try/finally construct with multiples, as I tend to declare things as close to possible to when i need them in code...
Excellent point Glenn!
Title: Re: AU 2005 - Example Code Project
Post by: BazzaCAD on July 17, 2006, 06:17:52 PM
Per request, attached are the VS2005 project files for my AU2005 code samples.  I *may* have an older VS2003 version of this project lying around if anyone is interested.  Just let me know.


Sorry, total noob here.
When trying to compile your code in VS2005 Express I get this error over & over again
"The type or namespace name 'Autodesk' could not be found (are you missing a using directive or an assembly reference?)"
Is this an issue with Express, or is there something in the code I need to change?

thx for the help.
Title: Re: AU 2005 - Example Code Project
Post by: MickD on July 17, 2006, 06:30:29 PM
Hi BazzaCAD, make sure you have your using statements at the top of your code file, eg. -

using System;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;

- add others as required. This lets the compiler know which libraries and namespaces you are using.

Also make sure you have any libraries 'referenced' in, right click on 'References' in your solution explorer and choose 'Add reference' and browse for the 2 autocad dll's in the acad directory required to build your project.
A screen shot -
Title: Re: AU 2005 - Example Code Project
Post by: BazzaCAD on July 17, 2006, 08:35:44 PM
OK I got it to compile now but I get a .DLL, I thought I'd get a .ARX. How do I load this into Acad?
Sorry for my ignorance, this is all greek to me  :-)
Title: Re: AU 2005 - Example Code Project
Post by: MickD on July 17, 2006, 08:46:15 PM
No prob's, an ARX file is basically only a normal dll with a different extension that AutoCAD recognises.

To load a .net dll, type NETLOAD and select your dll and away you go!
Title: Re: AU 2005 - Example Code Project
Post by: Kerry on July 17, 2006, 10:32:28 PM
Barry, Have a look at these

http://www.theswamp.org/index.php?topic=7903.0
http://www.theswamp.org/index.php?topic=7904.0
http://www.theswamp.org/index.php?topic=7919.0

Plus the tutorials by Mick and Bobby

... and These are the AutoDesk Labs for .NET
http://usa.autodesk.com/adsk/servlet/index?id=1911627&siteID=123112