void CurrentSelection_Changed(object sender, System.EventArgs e) { if (sender != null) { Document oDoc = sender as Document; if (oDoc.Title == "gatehouse.nwd") { //this is the new selection after selecting if (oDoc.CurrentSelection.SelectedItems.Count > 0) { ComApi.InwOpState9 oState = ComApiBridge.State; try { ComApi.InwOaPath3 oPath = oState.CurrentSelection.Paths()[1]; if ((oPath.Nodes().Last() as ComApi.InwOaNode).IsLayer) { //check whether the custom property tab has been added. string mytabname = "MyCustomTabUserName"; int customProTabIndex = 1; ComApi.InwGUIPropertyNode2 nodePropertiesOwner = oState.GetGUIPropertyNode(oPath, true) as ComApi.InwGUIPropertyNode2; ComApi.InwGUIAttribute2 customTab = null; foreach (ComApi.InwGUIAttribute2 nwAtt in nodePropertiesOwner.GUIAttributes()) { if (!nwAtt.UserDefined) { continue; } if (nwAtt.ClassUserName == mytabname) { customTab = nwAtt; break; } customProTabIndex += 1; } if (customTab == null) { ////create the tab if it does not exist ComApi.InwOaPropertyVec newPvec = (ComApi.InwOaPropertyVec)oState.ObjectFactory( ComApi.nwEObjectType.eObjectType_nwOaPropertyVec, null, null); ComApi.InwOaProperty prop1 = oState.ObjectFactory(ComApi.nwEObjectType.eObjectType_nwOaProperty) as ComApi.InwOaProperty; ComApi.InwOaProperty prop2 = oState.ObjectFactory(ComApi.nwEObjectType.eObjectType_nwOaProperty) as ComApi.InwOaProperty; prop1.name = "Finish"; prop1.UserName = "******"; object linkVal = m_dblink.read("Finish", (oPath.Nodes().Last() as ComApi.InwOaNode).UserName); if (linkVal != null) { prop1.value = linkVal; newPvec.Properties().Add(prop1); } prop2.name = "Notes"; prop2.UserName = "******"; linkVal = m_dblink.read("Notes", (oPath.Nodes().Last() as ComApi.InwOaNode).UserName); if (linkVal != null) { prop2.value = linkVal; newPvec.Properties().Add(prop2); } //the first argument is always 0 if adding a new tab nodePropertiesOwner.SetUserDefined(0, mytabname, mytabname, newPvec); } else { ////update the properties in the tab with the new values from database ComApi.InwOaPropertyVec newPvec = (ComApi.InwOaPropertyVec)oState.ObjectFactory( ComApi.nwEObjectType.eObjectType_nwOaPropertyVec, null, null); foreach (ComApi.InwOaProperty nwProp in customTab.Properties()) { ComApi.InwOaProperty prop = oState.ObjectFactory(ComApi.nwEObjectType.eObjectType_nwOaProperty) as ComApi.InwOaProperty; prop.name = nwProp.name; prop.UserName = nwProp.UserName; object linkVal = m_dblink.read(prop.name, (oPath.Nodes().Last() as ComApi.InwOaNode).UserName); if (linkVal != null) { prop.value = linkVal; newPvec.Properties().Add(prop); } } nodePropertiesOwner.SetUserDefined(customProTabIndex, mytabname, mytabname, newPvec); } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } } }
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 void Add_newTab(IEnumerable <ModelItem> items, string[] Lines, int N) { // Intialize the parameters ComApi.InwOpState9 oState = ComApiBridge.State; string[] OldCategory = new string[N]; string[] OldParameters = new string[N]; string[] NewParameters = new string[N]; string[] Parameters_hasAdded = new string[N]; int ind = 0; Boolean Parameter_isNotRepeated = true; ComApi.InwOaProperty[] NewProperties = new ComApi.InwOaProperty[N]; // Iterate over all the selected items foreach (ModelItem oItem in items) { if (oItem.Children.Count() > 0) { // Convert the .NET to COM object ComApi.InwOaPath oPath = ComApiBridge.ToInwOaPath(oItem); // Create new property category ComApi.InwOaPropertyVec newPvec = (ComApi.InwOaPropertyVec)oState.ObjectFactory(ComApi.nwEObjectType.eObjectType_nwOaPropertyVec, null, null); // Get properties collection of the path ComApi.InwGUIPropertyNode2 propn = (ComApi.InwGUIPropertyNode2)oState.GetGUIPropertyNode(oPath, true); // Iterate over all properties in textfile ind = 0; for (int i = 1; i < N; i++) { // Check if line is empty if (Lines[i] != null && Lines[i].Length > 1) { // Get Category, old property and new property from the line string[] WordsInLine = Lines[i].Split('\t'); OldCategory[i] = WordsInLine[0]; OldParameters[i] = WordsInLine[1]; NewParameters[i] = WordsInLine[2]; // By default the property is not repeated Parameter_isNotRepeated = true; // Iterate over properties of the item foreach (PropertyCategory oPC in oItem.PropertyCategories) { if (oPC.DisplayName == OldCategory[i]) { foreach (DataProperty oDP in oPC.Properties) { if (oDP.DisplayName == OldParameters[i]) { // Create new property NewProperties[i] = (ComApi.InwOaProperty)oState.ObjectFactory(ComApi.nwEObjectType.eObjectType_nwOaProperty, null, null); NewProperties[i].name = NewParameters[i]; NewProperties[i].UserName = NewParameters[i]; // Check if the property had been added Parameters_hasAdded[ind] = NewParameters[i]; ind = ind + 1; for (int d = 0; d < ind - 1; d++) { if (NewParameters[i] == Parameters_hasAdded[d]) { Parameter_isNotRepeated = false; } } // If the property had not added yet, add it if (Parameter_isNotRepeated) { // Check datatype of the property switch (oDP.Value.DataType) { case VariantDataType.Boolean: bool NewValueBool = oDP.Value.ToBoolean(); NewProperties[i].value = NewValueBool; break; case VariantDataType.DateTime: DateTime NewValueDate = oDP.Value.ToDateTime(); NewProperties[i].value = NewValueDate; break; case VariantDataType.DisplayString: string NewValueDString = oDP.Value.ToDisplayString(); NewProperties[i].value = NewValueDString; break; case VariantDataType.Double: double NewValueDouble = oDP.Value.ToDouble(); NewProperties[i].value = NewValueDouble; break; case VariantDataType.DoubleAngle: double NewValueAngle = oDP.Value.ToDoubleAngle(); NewProperties[i].value = NewValueAngle; break; case VariantDataType.DoubleArea: double NewValueArea = oDP.Value.ToDoubleArea(); NewProperties[i].value = NewValueArea * Math.Pow(meter_scale, 2); break; case VariantDataType.DoubleLength: double NewValueLength = oDP.Value.ToDoubleLength(); NewProperties[i].value = NewValueLength * meter_scale; break; case VariantDataType.DoubleVolume: double NewValueVolume = oDP.Value.ToDoubleVolume(); NewProperties[i].value = NewValueVolume * Math.Pow(meter_scale, 3); break; case VariantDataType.IdentifierString: string NewValueIString = oDP.Value.ToIdentifierString(); NewProperties[i].value = NewValueIString; break; case VariantDataType.Int32: int NewValueInt = oDP.Value.ToInt32(); NewProperties[i].value = NewValueInt; break; case VariantDataType.NamedConstant: NamedConstant NewValueConst = oDP.Value.ToNamedConstant(); String s = NewValueConst.DisplayName; s = s.Substring(s.IndexOf("\"") + 1); s = s.Substring(0, s.IndexOf("\"")); NewProperties[i].value = s; break; case VariantDataType.None: string NewValueNone = oDP.Value.ToString(); NewProperties[i].value = NewValueNone; break; case VariantDataType.Point3D: string NewValuePoint = oDP.Value.ToString(); NewProperties[i].value = NewValuePoint; break; } // Add the new property to the new property category newPvec.Properties().Add(NewProperties[i]); } } } } } } } // Add new tab to the object // The name of the newtab is saved in first line in the textfile propn.SetUserDefined(0, Lines[0], "MyAttribute", newPvec); } } }