private static void SetValues(ModelItem m, string CategoryName, string PropertyName, string value) { state = ComApiBridge.State; InwOaPath oPath = ComApiBridge.ToInwOaPath(m); // get properties collection of the path InwGUIPropertyNode2 propertyNode = state.GetGUIPropertyNode(oPath, true) as InwGUIPropertyNode2; // creating tab (Category), property null variables as placeholders InwGUIAttribute2 existingCategory = null; //Index of userDefined Tab int index = 1; //Case 1: Look for an existing category with the same CategoryName foreach (Autodesk.Navisworks.Api.Interop.ComApi.InwGUIAttribute2 attribute in propertyNode.GUIAttributes()) { if (attribute.UserDefined) { if (attribute.ClassUserName == CategoryName) { existingCategory = attribute; NavisProperties properties = new NavisProperties(PropertyName, value, existingCategory); setProperty(properties, index, propertyNode); return; } index++; } } //Case 2: Category doesn´t exist, create category and property if (existingCategory == null) { NavisProperties properties = new NavisProperties(PropertyName, value, CategoryName); setCategoryAndProperty(properties, propertyNode); return; } }
public override int Execute(params string[] parameters) { DebugMessage DebugP = new DebugMessage(); string EndMessage = ""; #region "Interop COM Setup" ///////////////////////////////////////////////////////////////////////////////////////////////// // .Net: Setup Document openDoc = Autodesk.Navisworks.Api.Application.ActiveDocument; Model model = openDoc.Models[0]; // .Net: get current selection from .NET API ModelItemCollection ObjectSelectionInNet = openDoc.CurrentSelection.SelectedItems; // .Net: get model item ModelItem ObjectsModelItem; try { ObjectsModelItem = openDoc.CurrentSelection.SelectedItems[0]; } catch { MessageBox.Show("No Objects Selected."); goto Exit; } ///////////////////////////////////////////////////////////////////////////////////////////////// // COM: Declare "InwOpState" (COM API Main Entry) Object ComApi.InwOpState InteropState = ComBridge.State; // Analogous To .Net "Document" // COM: Convert .Net Selection To COM Selection ComApi.InwOpSelection ObjectsSelectionInCOM = ComApiBridge.ToInwOpSelection(ObjectSelectionInNet); // COM: Convert .Net Path To Path In COM ComApi.InwOaPath ObjectsPathInCOM = ComApiBridge.ToInwOaPath(ObjectsModelItem); // convert to model item in .NET ModelItem ObjectsItemInNet = ComApiBridge.ToModelItem(ObjectsPathInCOM); #endregion // zoom in current view to selected objects ComApiBridge.State.ZoomInCurViewOnCurSel(); // ??? ComApi.InwOpState9 oStateAPI = ComApiBridge.State; //convert to COM selection ComApi.InwOpSelection CurrentGUISelection = ComApiBridge.ToInwOpSelection(ObjectSelectionInNet); // Debug Variables uint levelOneIterator = 0, levelTwoIterator = 0; // Takeoff Tracking Variables uint existingIDsCount = 0; string CurrentTime = DateTime.Now.ToString("h:mm:ss tt"); //ComApi.InwSelectionPathsColl ModelObjectsPaths = #region "Change Custom Properties" //get paths collection within the selection ComApi.InwSelectionPathsColl ObjectCurrentSelectPaths = CurrentGUISelection.Paths(); // Custom Properties Variables const string PropertiesNameString = "Custom_Properties"; const string NWAttPropIDNameString = "ID"; uint PropertyIndex = 0; // Used To Locate The Node For Updating Properties int IDPropertyIndex = 0; // Find Existing Custom Properties //foreach (ComApi.InwOaPath3 //add custom properties To Selected Objects // http://adndevblog.typepad.com/aec/2012/08/addmodifyremove-custom-attribute-using-com-api.html foreach (ComApi.InwOaPath3 ObjectPath in ObjectCurrentSelectPaths) { //// Make Sure All Children Are Iterated Through //foreach(ComApi.InwOaPath3 terminalPath in oPath) //{ } //get property categories ComApi.InwGUIPropertyNode2 NWPropertyNode = (ComApi.InwGUIPropertyNode2)oStateAPI.GetGUIPropertyNode(ObjectPath, true); //create a new property category ComApi.InwOaPropertyVec NewPropertyCategory = (ComApi.InwOaPropertyVec)oStateAPI.ObjectFactory( ComApi.nwEObjectType.eObjectType_nwOaPropertyVec, null, null); //create a new property ComApi.InwOaProperty NewProperty1 = (ComApi.InwOaProperty)oStateAPI.ObjectFactory( ComApi.nwEObjectType.eObjectType_nwOaProperty, null, null); NewProperty1.name = "Time"; NewProperty1.UserName = "******"; NewProperty1.value = CurrentTime; // Add To Category NewPropertyCategory.Properties().Add(NewProperty1); //create a new property ComApi.InwOaProperty NewProperty2 = (ComApi.InwOaProperty)oStateAPI.ObjectFactory( ComApi.nwEObjectType.eObjectType_nwOaProperty, null, null); NewProperty2.name = NWAttPropIDNameString; NewProperty2.UserName = NWAttPropIDNameString; NewProperty2.value = "ERROR: Unassigned ID"; // Add To Category //NewPropertyCategory.Properties().Add(NewProperty2); ////////////////////////////////////////////////////////////////////////////////////////// // Find User Defined GUIAttributes PropertyIndex = 0; // Reset Property Index (0 Indicates New Property Category) uint i = 0, j = 0; foreach (ComApi.InwGUIAttribute2 NWAttribute in NWPropertyNode.GUIAttributes()) { if (NWAttribute.UserDefined == true) { ++i;// Move Index Finder // If Particular Category Name (e.g. "Custom_Properties" Or Whatever PropertiesNameString Is Set To) if (NWAttribute.ClassUserName == PropertiesNameString) { PropertyIndex = i;// Set Index //DebugP.Send("NWAttribute.ClassUserName: "******"Loop Here Found: " + NWProperty.name.ToString()); } ++j; } } } } ////////////////////////////////////////////////////////////////////////////////////// try// Update Takeoff Time (Don't Change ID) If Property Exists { //DebugP.Send("Trying To Update With Index: " + PropertyIndex.ToString()); //DebugP.Send("Start Property Update"); //DebugP.Send("a1 IDPropertyIndex Value: " + IDPropertyIndex.ToString()); //DebugP.Send("a2 Current Value: " + NewProperty2.value.ToString()); NewProperty2.value = existingIDsCount.ToString(); //DebugP.Send("a3 ID Value Changed"); //DebugP.Send("a4 Current Value: " + NewProperty2.value.ToString()); //NewPropertyCategory.Properties().Remove(IDPropertyIndex); //DebugP.Send("a5 ID Value Changed"); //NewPropertyCategory.Properties().Clear();// Works //DebugP.Send("a6 Properties Category Cleared"); NewPropertyCategory.Properties().Add(NewProperty2); //DebugP.Send("a7 Properties Re-Added"); NWPropertyNode.SetUserDefined((int)PropertyIndex, PropertiesNameString, "Custom_Properties_InteralName", NewPropertyCategory); ++existingIDsCount; //DebugP.Send("a8 Update Properties Complete"); } catch (Exception e) { MessageBox.Show("Failed To Change Custom Properties.\nExiting.", "ERROR: Exception Catch"); goto Exit; } } #endregion ////////////////////////////////////////////////////////////////////////////////////// // Comb For Existing ID Objects // ?ERROR Possibility of Doubling Up Count Because Of Children Nodes //++existingIDsCount; // Show Location Of Working Directory //MessageBox.Show(Directory.GetCurrentDirectory()); System.IO.File.AppendAllText(@".\" + KANGVars.PrimitivesFileName + ".txt", "File Made At " + CurrentTime + "\n\n"); System.IO.File.AppendAllText(@".\" + KANGVars.OutputFileName + ".csv", "File Made At " + CurrentTime + "\n\nName,,X,Y,Z\n"); // create the callback object CallbackGeomListener callbkListener = new CallbackGeomListener(); foreach (ComApi.InwOaPath3 objectPath in CurrentGUISelection.Paths())// Each Object In The Selection As Distinguished By Individual Paths { // Properties KANGVars.strg = levelOneIterator.ToString(); foreach (PropertyCategory oPC in ObjectSelectionInNet[(int)levelOneIterator].PropertyCategories) { //DebugP.Send("Loop Value: " + oPC.DisplayName); } //MessageBox.Show("Begin Level1: " + levelOneIterator.ToString()); int TriangleExceptionCount = 0; foreach (ComApi.InwOaFragment3 frag in objectPath.Fragments())// Each Fragment Part Of Object { //DebugP.Send("Begin Level2: " + levelTwoIterator.ToString()); // generate the primitives frag.GenerateSimplePrimitives(ComApi.nwEVertexProperty.eNORMAL, callbkListener); try { callbkListener.Triangle(frag.Geometry, frag.Geometry, frag.Geometry); } catch (Exception e) { //MessageBox.Show("An Error Occurred Calling Callback Method: '{0}'", e.ToString()); // Keep Count Of These Exceptions? ++TriangleExceptionCount; if (TriangleExceptionCount > 1) { EndMessage = EndMessage + "\nERROR: Primitives Exceptions Exceed 1!"; //DebugP("ERROR: Primitives Exceptions Exceed 1!"); } } ++levelTwoIterator; } ++levelOneIterator; // XYZ Lengths From Min Max Values string XYZLengths = ""; float calcValue; calcValue = KANGlobalMinMaxCoords.maxX - KANGlobalMinMaxCoords.minX; XYZLengths = "," + calcValue.ToString(); calcValue = KANGlobalMinMaxCoords.maxY - KANGlobalMinMaxCoords.minY; XYZLengths = XYZLengths + "," + calcValue.ToString(); calcValue = KANGlobalMinMaxCoords.maxZ - KANGlobalMinMaxCoords.minZ; XYZLengths = XYZLengths + "," + calcValue.ToString() + "\n"; System.IO.File.AppendAllText(@".\" + KANGVars.OutputFileName + ".csv", KANGVars.strg + ","); System.IO.File.AppendAllText(@".\" + KANGVars.OutputFileName + ".csv", XYZLengths); #region Reset Global Min Max KANGlobalMinMaxCoords.minX = 100000; KANGlobalMinMaxCoords.maxX = -100000; KANGlobalMinMaxCoords.minY = 100000; KANGlobalMinMaxCoords.maxY = -100000; KANGlobalMinMaxCoords.minZ = 100000; KANGlobalMinMaxCoords.maxZ = -100000; #endregion // Reset Global Min Max } EndMessage = EndMessage + "\n Objects Evaluated: " + ObjectSelectionInNet.Count.ToString(); Exit: EndMessage = EndMessage + "\n\nPlugin Operation Exit."; MessageBox.Show(EndMessage); return(0); }
public static void Invoke() { if (!Check()) { return; } var modelItemEnumerableCollection = Application.ActiveDocument.Models.RootItemDescendantsAndSelf; foreach (ModelItem item in modelItemEnumerableCollection) { if (item.HasGeometry) { //转换为COM对象 Autodesk.Navisworks.Api.Interop.ComApi.InwOaPath3 inwOaPath = (InwOaPath3)ComApiBridge.ToInwOaPath(item); foreach (Autodesk.Navisworks.Api.Interop.ComApi.InwOaFragment3 inwOaFragment in inwOaPath.Fragments()) { Autodesk.Navisworks.Api.Interop.ComApi.InwOpState10 state = ComApiBridge.State; Autodesk.Navisworks.Api.Interop.ComApi.InwOaPath3 inwOaPath2 = (Autodesk.Navisworks.Api.Interop.ComApi.InwOaPath3)inwOaFragment.path; Autodesk.Navisworks.Api.Interop.ComApi.InwLTransform3f localToWorldMatrix = inwOaFragment.GetLocalToWorldMatrix(); //矩阵 Array array3 = (Array)(object)localToWorldMatrix.Matrix; //列向量 Matrix3D transformation = new Matrix3D((double)array3.GetValue(1), (double)array3.GetValue(2), (double)array3.GetValue(3), (double)array3.GetValue(4), (double)array3.GetValue(5), (double)array3.GetValue(6), (double)array3.GetValue(7), (double)array3.GetValue(8), (double)array3.GetValue(9), (double)array3.GetValue(10), (double)array3.GetValue(11), (double)array3.GetValue(12), (double)array3.GetValue(13), (double)array3.GetValue(14), (double)array3.GetValue(15), (double)array3.GetValue(16)); } } } }