KevinBarnett
2006-08-16, 09:30 AM
Greetings,
I admit it I am a newbie and I have not yet attended the C# course (will be soon). The following code runs fine in AutoCAD 2006, but when I exit - I always get the "Unhandled Exception" message. This cannot be healthy and I would nt like the other users to encounter this everytime they use my programs. Where did I go wrong? How do I prevent the error?
using System;
using System.Collections;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace MySpace
{
public class LineSelection
{
#region Public Variables
public enum CompareField
{
PointX,
PointY,
ObjID
}
#endregion
class ObjCoords
{
#region Private Variables
private double pX;
private double pY;
private long oID;
#endregion
#region Property
public double PointX
{
get
{
return pX;
}
set
{
pX = value;
}
}
public double PointY
{
get
{
return pY;
}
set
{
pY = value;
}
}
public long ObjID
{
get
{
return oID ;
}
set
{
oID = value;
}
}
#endregion
}
class CompareClass : IComparer
{
#region Private Variables
private CompareField sortBy = CompareField.PointX;
#endregion
#region Properties
public CompareField SortBy
{
get
{
return sortBy;
}
set
{
sortBy = value;
}
}
#endregion
public CompareClass()
{
//default constructor
}
public CompareClass(CompareField pSortBy)
{
sortBy = pSortBy;
}
public Int32 Compare(Object pFirstObject, Object pObjectToCompare)
{
if(pFirstObject is ObjCoords)
{
switch(this.sortBy)
{
case CompareField.PointX:
return Comparer.DefaultInvariant.Compare(((ObjCoords)pFirstObject).PointX,((ObjCoords)pObjectToCompare).PointX);
case CompareField.PointY:
return Comparer.DefaultInvariant.Compare(((ObjCoords)pFirstObject).PointY,((ObjCoords)pObjectToCompare).PointY);
case CompareField.ObjID:
return Comparer.DefaultInvariant.Compare(((ObjCoords)pFirstObject).ObjID,((ObjCoords)pObjectToCompare).ObjID);
default:
return 0;
}
}
else
return 0;
}
}
public static void Main()
{
System.Console.WriteLine("Hello, world!");
}
[CommandMethod("BigPLPAng")]
public void SelectSetExample()
{
Database DB = acadApp.DocumentManager.MdiActiveDocument.Database;
Autodesk.AutoCAD.DatabaseServices.TransactionManager TM = DB.TransactionManager;
Transaction TR = TM.StartTransaction();
Editor currentEditor = acadApp.DocumentManager.MdiActiveDocument.Editor;
PromptDoubleOptions opt1 = new PromptDoubleOptions("\nPoint Tolerance: ");
try
{
PromptDoubleResult PTol = currentEditor.GetDouble(opt1);
if (PTol.Status != PromptStatus.OK)
{
return;
}
SelectionAddedEventHandler selectionAdded = new SelectionAddedEventHandler(OnSelectionAdded);
currentEditor.SelectionAdded += selectionAdded;
PromptSelectionOptions ssOptions = new PromptSelectionOptions();
ssOptions.AllowDuplicates = true;
ssOptions.MessageForAdding = "\nSelect the Lines: ";
PromptSelectionResult ssResult = currentEditor.GetSelection(ssOptions);
currentEditor.SelectionAdded -= selectionAdded;
if (ssResult.Status != PromptStatus.OK)
{
return;
}
ObjCoords[] items1 = new ObjCoords[ssResult.Value.Count * 2];
Int32 index11 = -1;
foreach (SelectedObject so in ssResult.Value)
{
Line obj = TM.GetObject(so.ObjectId, OpenMode.ForRead) as Line;
index11 = index11 + 1;
items1[index11] = new ObjCoords();
items1[index11].PointX = obj.StartPoint.X;
items1[index11].PointY = obj.StartPoint.Y;
items1[index11].ObjID = obj.ObjectId.OldId ;
index11 = index11 + 1;
items1[index11] = new ObjCoords();
items1[index11].PointX = obj.EndPoint.X;
items1[index11].PointY = obj.EndPoint.Y;
items1[index11].ObjID = obj.ObjectId.OldId;
}
CompareClass compareType;
compareType = new CompareClass(CompareField.PointX);
SortObjects(compareType,true,items1);
index11 = 0;
acadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nAfter Sorting on X...");
foreach(ObjCoords item in items1)
{
acadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nS.No: {0} :: X: {1} :: Y: {2} :: ID: {3}", index11.ToString(),item.PointX, item.PointY, item.ObjID);
index11 ++;
}
}
catch(Autodesk.AutoCAD.Runtime.Exception ex)
{
acadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nError: " + ex.Message);
}
}
private void OnSelectionAdded(object sender, SelectionAddedEventArgs e)
{
using (Transaction ssTrans = acadApp.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction())
{
ObjectId[] selectedEntityIds = e.AddedObjects.GetObjectIds();
Entity selectedEntity;
for (int entityIndex = 0; entityIndex < e.AddedObjects.Count; entityIndex++)
{
ObjectId selectedEntityId = selectedEntityIds[entityIndex];
selectedEntity = (Entity)ssTrans.GetObject(selectedEntityId, OpenMode.ForRead);
if (selectedEntity is Line)
{
// check for xdata...
}
else
{
e.Remove(entityIndex);
}
}
}
}
private static void SortObjects(CompareClass pSortBy, Boolean pSortAscending, Array items)
{
Array.Sort(items,pSortBy);
if(!pSortAscending)Array.Reverse(items);
}
}
}
[ Moderator Action = ON ] Why does my CODE not format properly? (http://forums.augi.com/misc.php?do=bbcode#code) [ Moderator Action = OFF ]
I admit it I am a newbie and I have not yet attended the C# course (will be soon). The following code runs fine in AutoCAD 2006, but when I exit - I always get the "Unhandled Exception" message. This cannot be healthy and I would nt like the other users to encounter this everytime they use my programs. Where did I go wrong? How do I prevent the error?
using System;
using System.Collections;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace MySpace
{
public class LineSelection
{
#region Public Variables
public enum CompareField
{
PointX,
PointY,
ObjID
}
#endregion
class ObjCoords
{
#region Private Variables
private double pX;
private double pY;
private long oID;
#endregion
#region Property
public double PointX
{
get
{
return pX;
}
set
{
pX = value;
}
}
public double PointY
{
get
{
return pY;
}
set
{
pY = value;
}
}
public long ObjID
{
get
{
return oID ;
}
set
{
oID = value;
}
}
#endregion
}
class CompareClass : IComparer
{
#region Private Variables
private CompareField sortBy = CompareField.PointX;
#endregion
#region Properties
public CompareField SortBy
{
get
{
return sortBy;
}
set
{
sortBy = value;
}
}
#endregion
public CompareClass()
{
//default constructor
}
public CompareClass(CompareField pSortBy)
{
sortBy = pSortBy;
}
public Int32 Compare(Object pFirstObject, Object pObjectToCompare)
{
if(pFirstObject is ObjCoords)
{
switch(this.sortBy)
{
case CompareField.PointX:
return Comparer.DefaultInvariant.Compare(((ObjCoords)pFirstObject).PointX,((ObjCoords)pObjectToCompare).PointX);
case CompareField.PointY:
return Comparer.DefaultInvariant.Compare(((ObjCoords)pFirstObject).PointY,((ObjCoords)pObjectToCompare).PointY);
case CompareField.ObjID:
return Comparer.DefaultInvariant.Compare(((ObjCoords)pFirstObject).ObjID,((ObjCoords)pObjectToCompare).ObjID);
default:
return 0;
}
}
else
return 0;
}
}
public static void Main()
{
System.Console.WriteLine("Hello, world!");
}
[CommandMethod("BigPLPAng")]
public void SelectSetExample()
{
Database DB = acadApp.DocumentManager.MdiActiveDocument.Database;
Autodesk.AutoCAD.DatabaseServices.TransactionManager TM = DB.TransactionManager;
Transaction TR = TM.StartTransaction();
Editor currentEditor = acadApp.DocumentManager.MdiActiveDocument.Editor;
PromptDoubleOptions opt1 = new PromptDoubleOptions("\nPoint Tolerance: ");
try
{
PromptDoubleResult PTol = currentEditor.GetDouble(opt1);
if (PTol.Status != PromptStatus.OK)
{
return;
}
SelectionAddedEventHandler selectionAdded = new SelectionAddedEventHandler(OnSelectionAdded);
currentEditor.SelectionAdded += selectionAdded;
PromptSelectionOptions ssOptions = new PromptSelectionOptions();
ssOptions.AllowDuplicates = true;
ssOptions.MessageForAdding = "\nSelect the Lines: ";
PromptSelectionResult ssResult = currentEditor.GetSelection(ssOptions);
currentEditor.SelectionAdded -= selectionAdded;
if (ssResult.Status != PromptStatus.OK)
{
return;
}
ObjCoords[] items1 = new ObjCoords[ssResult.Value.Count * 2];
Int32 index11 = -1;
foreach (SelectedObject so in ssResult.Value)
{
Line obj = TM.GetObject(so.ObjectId, OpenMode.ForRead) as Line;
index11 = index11 + 1;
items1[index11] = new ObjCoords();
items1[index11].PointX = obj.StartPoint.X;
items1[index11].PointY = obj.StartPoint.Y;
items1[index11].ObjID = obj.ObjectId.OldId ;
index11 = index11 + 1;
items1[index11] = new ObjCoords();
items1[index11].PointX = obj.EndPoint.X;
items1[index11].PointY = obj.EndPoint.Y;
items1[index11].ObjID = obj.ObjectId.OldId;
}
CompareClass compareType;
compareType = new CompareClass(CompareField.PointX);
SortObjects(compareType,true,items1);
index11 = 0;
acadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nAfter Sorting on X...");
foreach(ObjCoords item in items1)
{
acadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nS.No: {0} :: X: {1} :: Y: {2} :: ID: {3}", index11.ToString(),item.PointX, item.PointY, item.ObjID);
index11 ++;
}
}
catch(Autodesk.AutoCAD.Runtime.Exception ex)
{
acadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nError: " + ex.Message);
}
}
private void OnSelectionAdded(object sender, SelectionAddedEventArgs e)
{
using (Transaction ssTrans = acadApp.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction())
{
ObjectId[] selectedEntityIds = e.AddedObjects.GetObjectIds();
Entity selectedEntity;
for (int entityIndex = 0; entityIndex < e.AddedObjects.Count; entityIndex++)
{
ObjectId selectedEntityId = selectedEntityIds[entityIndex];
selectedEntity = (Entity)ssTrans.GetObject(selectedEntityId, OpenMode.ForRead);
if (selectedEntity is Line)
{
// check for xdata...
}
else
{
e.Remove(entityIndex);
}
}
}
}
private static void SortObjects(CompareClass pSortBy, Boolean pSortAscending, Array items)
{
Array.Sort(items,pSortBy);
if(!pSortAscending)Array.Reverse(items);
}
}
}
[ Moderator Action = ON ] Why does my CODE not format properly? (http://forums.augi.com/misc.php?do=bbcode#code) [ Moderator Action = OFF ]