-
February 16th, 2013, 01:54 AM
#1
C# Code for Autocad
I'm hoping this is a good place to come to get help with C#, no matter what API it's used with. I am trying to learn C# for personal use with Autodesk's Autocad. There are forums for Autocad specific questions, but only a very few who know the API are helpful...the rest are total pricks. I'm really hoping C# programmers in this forum would be willing to offer better input. I've written the following code. Please offer any advice you are willing to give. If not, thank you anyway.
Code:
namespace BCCSLOPETAG
{
public class DraftingTools
{
[CommandMethod("BCC:GRS")]
public void BCCGRS()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
SelectionFilter sf = new SelectionFilter(new TypedValue[1] { new TypedValue((int)DxfCode.Start, "INSERT") });//<<<--- a selection filter to get only block references
try
{
while (true)
{
PromptEntityOptions peo = new PromptEntityOptions("\nSelect grade:");
peo.SetRejectMessage("\nMust be a block.");
peo.AddAllowedClass(typeof(BlockReference), false);
PromptEntityResult psr = ed.GetEntity(peo);
if (psr.Status != PromptStatus.OK)
return;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
ObjectId brId = psr.ObjectId;
// Open the block reference
BlockReference br = (BlockReference)tr.GetObject(brId, OpenMode.ForRead);
var att1 = br.Position;
ed.WriteMessage("\n{0}, Position: {1}", br.Name, att1);
}
PromptEntityOptions peo2 = new PromptEntityOptions("\nSelect grade:");
peo2.SetRejectMessage("\nMust be a block.");
peo2.AddAllowedClass(typeof(BlockReference), false);
PromptEntityResult psr2 = ed.GetEntity(peo2);
if (psr2.Status != PromptStatus.OK)
return;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
ObjectId brId2 = psr2.ObjectId;
// Open the block reference
BlockReference br2 = (BlockReference)tr.GetObject(brId2, OpenMode.ForRead);
var att2 = br2.Position;
ed.WriteMessage("\n{0}, Position: {1}", br2.Name, att2);
}
}
}
catch (System.Exception e)
{ Application.ShowAlertDialog(String.Format("Brian Says You Failed Horribly :P\n{0}", e.Message)); } //<<<----- This function will return if the user presses escape: this code is only reached on catastrophic failure
}
}
}
-
February 16th, 2013, 08:45 AM
#2
Re: C# Code for Autocad
You are going to need to be a bit more specific on what you are looking for. First of all, does the code you posted work? If it does work, what are you looking for in terms of advice? Structure? Layout? Different approach? What don't you like about your code?
-
February 16th, 2013, 05:34 PM
#3
Re: C# Code for Autocad
Originally Posted by Arjay
You are going to need to be a bit more specific on what you are looking for. First of all, does the code you posted work? If it does work, what are you looking for in terms of advice? Structure? Layout? Different approach? What don't you like about your code?
Thanks. The code works. Looking for advice on areas that might stand out being not "standard practice" in regards to C# coding. If it's impossible to know that without know the API then I might be SOL :/
-
February 16th, 2013, 05:49 PM
#4
Re: C# Code for Autocad
Critiquing the code is a bit difficult without fully knowing the requirements of the code and how robust it needs to be. That being said, here are a few general comments:
Your while(true) loop may be a bit of a concern. Is it important to recover from a non-Ok status and continue? If so, you may want to move the try/catch block inside the while loop.
Is the BCCGRS() method run inside a secondary thread? If so, pay attention to marking the thread as IsBackground = true; otherwise, you may run into trouble when shutting down the app (i.e. a while loop inside an IsBackGround = false thread may prevent the app from shutting down cleanly).
Can the BCCGRS() method be made static?
-
February 16th, 2013, 06:03 PM
#5
Re: C# Code for Autocad
Thanks Arjay...the comments / questions are just what I'm looking for...
I'm not savvy enough yet to answer the questions you asked but I'll definitely look them up as they are related to Autocad and my routine to see how they impact my addin.
Much appreciated!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|