See the top rated post in this thread. Click here

Results 1 to 6 of 6

Thread: Unhandled exception on Exit

  1. #1
    100 Club KevinBarnett's Avatar
    Join Date
    2001-10
    Location
    Pretoria, South Africa
    Posts
    119
    Login to Give a bone
    0

    Question Unhandled exception on Exit

    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?


    Code:
    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? [ Moderator Action = OFF ]
    Attached Files Attached Files
    Last edited by Opie; 2006-08-17 at 03:42 PM. Reason: [CODE] tags added, see moderator comment

  2. #2
    Member
    Join Date
    2003-10
    Posts
    43
    Login to Give a bone
    1

    Default Re: Unhandled exception on Exit

    Try This
    //added TR.Dispose();

    Code:
    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++;
            }
    
            //Dispose
            TR.Dispose(); 
    
          }
          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);
        }
    
      }
    
    }

  3. #3
    100 Club KevinBarnett's Avatar
    Join Date
    2001-10
    Location
    Pretoria, South Africa
    Posts
    119
    Login to Give a bone
    0

    Thumbs up Re: Unhandled exception on Exit

    You Got It!
    Thanks!

  4. #4
    Member
    Join Date
    2004-12
    Posts
    18
    Login to Give a bone
    0

    Default Re: Unhandled exception on Exit

    Quote Originally Posted by KevinBarnett
    You Got It!
    Thanks!
    Then the the time to mention this in the other post you have running on
    this in the Autodesk .net group. If you ask for others time then be polite
    and let them know when you get an answer.

  5. #5
    100 Club KevinBarnett's Avatar
    Join Date
    2001-10
    Location
    Pretoria, South Africa
    Posts
    119
    Login to Give a bone
    0

    Unhappy Re: Unhandled exception on Exit

    oops!! Sorry prichardson - just added the answer under:

    http://discussion.autodesk.com/forum.jspa?forumID=152

    Thx for mentioning it.

  6. #6
    Member
    Join Date
    2004-12
    Posts
    18
    Login to Give a bone
    0

    Default Re: Unhandled exception on Exit

    Quote Originally Posted by KevinBarnett
    oops!! Sorry prichardson - just added the answer under:

    http://discussion.autodesk.com/forum.jspa?forumID=152

    Thx for mentioning it.
    No reason to be sorry. Glad you got your problem solved

    Paul

Similar Threads

  1. Unhandled Exception
    By desaia in forum AutoCAD General
    Replies: 3
    Last Post: 2007-11-08, 01:42 PM
  2. Unhandled Exception
    By Steve_Bennett in forum ACA General
    Replies: 5
    Last Post: 2007-03-21, 08:15 PM
  3. Unhandled Exception
    By john.65869 in forum CAD Management - General
    Replies: 4
    Last Post: 2004-12-16, 12:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •