.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Discussion Group ClassLibrary, please read!

16 REPLIES 16
Reply
Message 1 of 17
Irvin
3307 Views, 16 Replies

Discussion Group ClassLibrary, please read!

Hi all,

 

In order to help out programmers starting to program AutoCAD Addins I’ve created a class library to make our lives a bit easier. I also hope other people here will contribute to this class library so we can create an AutoCAD frame work off building blocks. We all have to create layers, load line types create text styles and dimension styles.

 

At this moment I created a Layer Class, LineType Class and a general Enum Class So we can use StrongType names instead of text strings. I only added an enums for all Linetypes in acad.lin.

 

ClassDiagram.jpg

 

But I can think of some other enums like color witch returns color numbers.

 

The Second project in the solution is for testing and has some examples on how to use the classes.

 

project AcDg.jpg

 

The Solution is created in Visual Studio 2010. The AutoCAD version I used is AutoCAD 2011 and is  coded in C#. My knowledge of VB.NET is limited but it would be nice if there is someone who could convert It.

 

For the next versions I will add classes for creating text styles and dimension styles.

 

If any of you have comments about my code please don’t hesitate to tell me about it or change the code.

 

If anyone has an idée to keep the source on a central place please tell me so we can place it there. Some kind of versioning would be nice. For now please attach your classes with your post and I will add them to the solution and post a new version of it.

 

I hope we as community can create something we all can use. And make developing for AutoCAD a bit Easier.

 

Kind regards,

 

Irvin

16 REPLIES 16
Message 2 of 17
Irvin
in reply to: Irvin

Hello all,

 

I've added a new class to the solution.

 

AcDgTextStyle.cs

 

Kind regards,

 

Irvin

Message 3 of 17
Irvin
in reply to: Irvin

Hi All,

 

Had a little sparetime this weekend so I added a new class for inserting blocks.

 

AcDgBlock - Class.jpg

 

I’ve created a new control AcDgDwgThumbnail derived from the standard picturebox. I added 2 new properties to this control as you can see in the following picture.

 

AcDgwThumbnailPreviewProp.jpg

 

The DwgFile takes a string value witch hold a dwg including it’s path. If the file exists the dwgthumbnail wil be displayed in the AcDgDwgThumbnail control.

 

In the test application I added a simple function to display a dwg’s preview. See printscreen.

 

Thumbnail.jpg

 

I've also added the new Solution V1.02.

 

Kind regards,

 

Irvin

Message 4 of 17
Jeffrey_H
in reply to: Irvin

Nice Irvin,

 

A good issue to figure out is how to handle Transaction management.

 

And if I may add,

you probably or definitely need some type of management.

So the class library will mold nicely.

 

And some standards would need to be added.

 

Just say you got group of smart guys.

They build great classes but one did one for layers and his function names are AddLayer to add to database.

Another guy did text and his is CreateText but he has a function called AddText but that is to add it to some static collection or something.---You see where that is going

 

Here is how a current project for Lisp is being handled for some ideas.

 

 

Just go here http://www.theswamp.org/index.php?topic=37684.90 and enter "Group Project: Change item(s) to Layer"

in search. There are too man threads--- ideas, sign-up, vote....etc

 

 

 

 

You can also find your answers @ TheSwamp
Message 5 of 17
Irvin
in reply to: Irvin

Hi all,

 

I have been busy rewriting my classes and tought it was about time to share theme with you all. I've structered the code so they all work the same way. Here is the new AcDgClassLibrary - Classdiagram.

 

AcDgLibrary -   ClassDiagram.png

 

Fot the next version i will create an new class for creating new dimensionstyles ad a block jigging class and wil try to document my code as best as i can.

 

Jeff could you explain what mean with how to handle transactions?

 

For now i hope this will help people here and i will continue work on this project and hop more people will be involved.

 

If you have wishes please post them.

 

I've changed the version number to 1.1.0.0

 

Kind regards,

 

Irvin

Message 6 of 17
Jeffrey_H
in reply to: Irvin

To keep the user from having to deal with transactions and at the same time handle different databases, methods called in loops.....etc

 

You can also find your answers @ TheSwamp
Message 7 of 17
Irvin
in reply to: Irvin

Hi Jeff,

 

Do you have any ideas on how to handle that??? If so please let me know. I think we can win alot if we can create something like that.

 

Kind regards,

 

Irvin

Message 8 of 17
Oak3s
in reply to: Irvin

"In order to help out programmers starting to program AutoCAD Addins..."

 

For those of us (Me) who are starting, a readme file of some sort would help just to get this going. What to do and where to start (after download and extract of course) 🙂

 

FWIW [comming from a beginners perspective] Awesome work by the way.

Message 9 of 17
chillme1
in reply to: Oak3s

I second your suggestion. Also, I would love to have a version written in / converted to VB.NET code.

 

I have just finished a beginning AutoCAD VB.NET class (4-day, live or 4 hour live online sessions includes a book, many code snippets and instruction with a custom project day. It is around $1,000 and HIGHLY RECOMMENDED! See www.vbcad.com )

Thanks for your comments,

Clint Hill
Message 10 of 17
Dale.Bartlett
in reply to: Irvin

Irvin, This is a great initiative, thank you. I have a question regarding best practices. I note that you pass both the document and transaction to the class. Are you able to explain the reason for this? I generally pass the database, then start the transaction within the function. This is fundamental to building a reusable library so I would like to understand the best approach. Thanks, Dale.




______________
Yes, I'm Satoshi.
Message 11 of 17


@dbartlett wrote:

 I generally pass the database, then start the transaction within the function. This is fundamental to building a reusable library so I would like to understand the best approach. Thanks, Dale.


Not really.

 

To make code reusable, you should avoid any creation or comitting of transactions from within APIs that you want to reuse.  The best approach is to write the API with the assumption that the caller has started and is managing the transaction. 

 

From within called APIs, you just get the TransactionManager from the Database you're working with and use it to open objects or add new objects to the drawing. 

 

I have a farily extensive library of reusable code and none of my methods start or commit transactions. That's always done at the outer-most level (for example, in a custom command, it would be done by the implementation of the command handler with the CommandMethod attribute).

Message 12 of 17

I can understand the value of having the transactions outside of the library. What about passing document or database? The examples given with this post pass the document, then retrieve the database within the api. To use the library in a batch process, or possibly in a RealDWG application, wouldn't passing the database be a better option? Tony's comments here: http://forums.autodesk.com/t5/NET/WorkingDatabase-vs-MdiActiveDocument-Database/m-p/2455039/highligh... 

Thanks very much for the response. Dale  




______________
Yes, I'm Satoshi.
Message 13 of 17

Hi

I'm not quite sure about your approach for one simple reason: what happens if one method in one class calls another method in another class and both of them require a transaction being already started?

 

It is probably just me not really understanding the whole concept of when and where transactions should be started, so I would greatly appreciate if you could add some further explanations to your post.

Thanks

Message 14 of 17
_gile
in reply to: TobyRosenberg8728

Hi,

 

I agree with DiningPhiliosopher and have too a quite large libraries of extension methods which have to be called from within a transaction.

 

If needed, the Database.TransactionManager or TopTransaction can be get from the Database passed as argument, but most of the time this argument too is not necessary and the Database can be get from any database resident DBObject or ObjectId passed to the method.

 

The method can handle the fact it is called from within a transaction or not throwing a Exception.

 

Adding some Xml comments may also help the user as they display with the intellisense.

 

Here's a little and simple example of an extension method to get a block reference 'effective name' which have to be called from within a transaction this way :

blockRef.GetEffectiveName()

 

        /// <summary>
        /// Gets the block effective name (have to be called from a transaction).
        /// </summary>
        /// <param name="br">The BlockReference instance the method applies to.</param>
        /// <returns>The block effective name.</returns>
        public static string GetEffectiveName(this BlockReference br)
        {
            if (br.IsDynamicBlock)
            {
                Database db = br.Database;
                if (db == null)
                {
                    throw new Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.NotInDatabase);
                }

                Transaction tr = db.TransactionManager.TopTransaction;
                if (tr == null)
                {
                    throw new Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.NotTopTransaction);
                }

                BlockTableRecord btr =
                    (BlockTableRecord)tr.GetObject(br.DynamicBlockTableRecord, OpenMode.ForRead);
                return btr.Name;
            }
            return br.Name;
        }

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 15 of 17


@TobyRosenberg8728 wrote:

Hi

I'm not quite sure about your approach for one simple reason: what happens if one method in one class calls another method in another class and both of them require a transaction being already started?

 

It is probably just me not really understanding the whole concept of when and where transactions should be started, so I would greatly appreciate if you could add some further explanations to your post.

Thanks


Whether methods are in the same or different classes has little to do with transactions. You place methods in classes based on more basic functional requirements, not on whether there is or isn't an active transaction.  I have methods that make calls to other methods, both in the same class as well as in other classes. All of them assume that there's an active transaction in cases where they access database objects.

 

The best place for managing transactions is at the outer-most or highest levels, because if the code fails, then any active transacation will be aborted and changes will be rolled back. If you instead start using nested transactions, then things become much more complicated much more quickly.  I rarely use nested transactions when I write code that modifies databases, as it's usually unnecessary.

Message 16 of 17

Hi All, There wasn't a reply regarding whether to pass Document or Database:

What about passing document or database? The examples given with this post pass the document, then retrieve the database within the api. To use the library in a batch process, or possibly in a RealDWG application, wouldn't passing the database be a better option?

Can someone comment? I would like to proceed with developing a utility library, and this discussion has been very helpful. Thanks, Dale 




______________
Yes, I'm Satoshi.
Message 17 of 17

Hi All, I still haven't come to a conclusion re whether to pass Document or Database:
What about passing document or database? The examples given with this post pass the document, then retrieve the database within the api. To use the library in a batch process, or possibly in a RealDWG application, wouldn't passing the database be a better option?
Thanks, Dale



______________
Yes, I'm Satoshi.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost