0 Members and 1 Guest are viewing this topic.
[CommandMethod("JayTest_FindDupHandleNotChkUniqueId")]public void FindDupHandleNotChkUniqueId() // Find duct items in the drawing that have duplicated Item.Handle. // Here, we don't check Item.UniqueId. { Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; // Construct a list of Item.Handle of duct items on the drawing. List<int> lisHandle = new List<int>(); foreach( Item itm in Job.Items ) { lisHandle.Add( (int)itm.Handle ); } // Sort the list of Item.Handle. lisHandle.Sort(); // Find duplicated Item.Handle in the list. int nPrevHandle = -1; int nCurDupHandleCount = 0; // 0 - Not dealing with dup handle. // > 0 - Dealing with a dup handle. foreach( int nCurrHandle in lisHandle ) { // Take care of the special case when we are in the first entry. if ( nPrevHandle == -1 ) { nPrevHandle = nCurrHandle; // No need to check. continue; } // Here, we know that we are after the first entry. if ( nPrevHandle == nCurrHandle ) { if ( nCurDupHandleCount == 0 ) nCurDupHandleCount = 2; // 2 items have the same handle. else nCurDupHandleCount++; } else // Current handle is different from the handle in previous entry. { // Show the info about the duplicated handle (if any) that we // are dealing with before we reach the current one. if ( nCurDupHandleCount > 0 ) { String sMsg = "\nError: " + nCurDupHandleCount.ToString() + " items have the same Item.Handle " + nPrevHandle.ToString(); doc.Editor.WriteMessage( sMsg ); nCurDupHandleCount = 0; } // No duplicated. Get ready to check next one. nPrevHandle = nCurrHandle; } } // foreach() // Show the info about the duplicated handle (if any) that we are // dealing with before we reach the current one. This means the last // handle in the list is a duplicated handle. if ( nCurDupHandleCount > 0 ) { String sMsg = "\nError: " + nCurDupHandleCount.ToString() + " items have the same Item.Handle " + nPrevHandle.ToString(); doc.Editor.WriteMessage( sMsg ); nCurDupHandleCount = 0; } } // FindDupHandleNotChkUniqueId()
Error: 2 items have the same Item.Handle 9361Error: 3 items have the same Item.Handle 9371Error: 2 items have the same Item.Handle 9386Error: 2 items have the same Item.Handle 9413
[CommandMethod("JayTest_SelectDuctByHandle")]public void SelectDuctByHandle() // Select duct by their Item.Handle. This program first asks the // user to enter a Item.Handle. Then this program will select the // items that have the specified Item.Handle. { Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; Autodesk.AutoCAD.DatabaseServices.Database db = doc.Database; // Prompt the user for an Item.Handle. String sHandleFromUser = this.sPromptUserForHandle(); if ( sHandleFromUser.Length == 0 ) return; // Select the items that have the specified Item.Handle. List<ObjectId> lisSelectObjId = new List<ObjectId>(); foreach( Item itm in Job.Items ) { // Save the ObjectId of the current item to the list if the // current item has the specified Item.Handle. String sCurItemHandle = ( (int)itm.Handle ).ToString(); if ( sHandleFromUser.Equals( sCurItemHandle ) ) { String sCurAcadHandle = Job.GetACADHandleFromItem( itm ); if ( ! String.IsNullOrEmpty( sCurAcadHandle ) ) { // Change the handle from a string into a 64-bit long integer. long lCurAcadHandle = Convert.ToInt64( sCurAcadHandle, 16 ); // Convert the 64-bit long integer into a handle. Handle hCurItem = new Handle( lCurAcadHandle ); // Get an ObjectId from the handle. // Note: false to indicate that we don't want to create a // new object if there is no existing object that has // the specified handle. // Note: 0 is placed there for future use by Autodesk. ObjectId idCurItemObj = db.GetObjectId( false, hCurItem, 0 ); // Save the ObjectId of the current item to the list. lisSelectObjId.Add( idCurItemObj ); } } } // foreach Autodesk.AutoCAD.Internal.Utils.SelectObjects( lisSelectObjId.ToArray() ); } // SelectDuctByHandle()private String sPromptUserForHandle() // Ask the user to enter an Item.Handle. { Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; // Get the Item.Handle from the user. Repeat until the user has // entered an valid Item.Handle or has decided not to enter any more. String sHandleFromUser = ""; PromptStringOptions optPromptHandle = new PromptStringOptions( "\nEnter an Item.Handle: " ); optPromptHandle.AllowSpaces = false; do { PromptResult resPromptHandle = ed.GetString( optPromptHandle ); if ( resPromptHandle.Status != PromptStatus.OK ) return( "" ); // The user decides not to enter. OK. sHandleFromUser = resPromptHandle.StringResult.Trim().ToUpper(); if ( sHandleFromUser.Length == 0 ) return( "" ); // The user decides not to enter. OK. // Make sure the Item.Handle is all numeric. // ...skipped... } while( sHandleFromUser.Length == 0 ); // Return the Item.Handle to the calling program. return( sHandleFromUser ); }
When I try this to select items with Item.Handle 6371 using my test drawing, I find that actually only one item is selected. This seems to me that there is only one item with Item.Handle 6371 on the drawing. But Item.Handle 6371 appears 3 times in Job.Items.
public static bool IsFabItem(ObjectId OID) { return OID.ObjectClass.Name == "MapsContainer"; }
public static Item GetItemFromObjectID(ObjectId OID) { return Job.GetFabricationItemFromACADHandle(OID.Handle.ToString()); }