//Creates lastValueProperties Property Set which stores fields and values for last value method private void constructFieldArray() { // Assume we are in an edit session if (ArcMap.Editor.EditWorkspace == null) { return; } // Open defaults table IDataset dataset = m_dynDefaults as IDataset; IQueryFilter qFilter = new QueryFilterClass(); qFilter.WhereClause = "ValueMethod = 'LAST_VALUE'"; lastValueProperties = new PropertySetClass(); if (m_dynDefaults != null) { ICursor tabCursor = m_dynDefaults.Search(qFilter, true); IRow row = tabCursor.NextRow(); while (row != null) { object objRow = row.get_Value(dynTargetField); object nullObject = null; lastValueProperties.SetProperty(objRow.ToString(), nullObject); row = tabCursor.NextRow(); } } }
//Creates lastValueProperties Property Set which stores fields and values for last value method private void constructFieldArray() { // Assume we are in an edit session if (ArcMap.Editor.EditWorkspace == null) return; // Open defaults table IDataset dataset = m_dynDefaults as IDataset; IQueryFilter qFilter = new QueryFilterClass(); qFilter.WhereClause = "ValueMethod = 'LAST_VALUE'"; lastValueProperties = new PropertySetClass(); if (m_dynDefaults != null) { ICursor tabCursor = m_dynDefaults.Search(qFilter, true); IRow row = tabCursor.NextRow(); while (row != null) { object objRow = row.get_Value(dynTargetField); object nullObject = null; lastValueProperties.SetProperty(objRow.ToString(), nullObject); row = tabCursor.NextRow(); } } }
private void setDynamicDefaults(IObject inObject, string mode) { try { //Convert row to feature (test for feature is null before using - this could be a table update) IFeature inFeature = inObject as IFeature; // Skip Orphan Junctions (saves time) if (inFeature != null) { INetworkFeature inNetFeat = inObject as INetworkFeature; if ((inNetFeat != null) && (inFeature.Class.ObjectClassID == inNetFeat.GeometricNetwork.OrphanJunctionFeatureClass.ObjectClassID)) { return; } } //Get cursor to dynamic values table retriving only ICursor tabCursor; getDefaultRows(inObject, out tabCursor); IRow row = null; if (tabCursor != null) { row = tabCursor.NextRow(); } //for each row in the matching rows (matched by table name or wildcard) returned from the config table while (row != null) { //get fieldname string fieldName = row.get_Value(dynTargetField).ToString(); //if this field is found in the feature/object being added or modified... int fieldNum = inObject.Fields.FindField(fieldName); if (fieldNum > -1) { // get requested method and any data parameters string valMethod = row.get_Value(dynMethodField).ToString(); string valData = row.get_Value(dynDataField).ToString(); switch (mode) { case "OnCreate": //Continue to next field in config table if create events were not requested if (row.get_Value(dynCreateField).ToString() == "0") { row = tabCursor.NextRow(); continue; } break; case "OnChange": // Collect value for changed feature (stored for LAST VALUE method) IRowChanges inChanges = inObject as IRowChanges; bool changed = inChanges.get_ValueChanged(fieldNum); if (changed) { lastValueProperties.SetProperty(fieldName, inObject.get_Value(fieldNum)); } //Continue to next field in config table if change events were not requested if (row.get_Value(dynChangeField).ToString() == "0") { row = tabCursor.NextRow(); continue; } break; } // set values as specified switch (valMethod) { case "TIMESTAMP": inObject.set_Value(fieldNum, DateTime.Now); break; case "LAST_VALUE": if (mode == "OnCreate") { //if (inObject.get_Value(fieldNum) == null) //{ object lastValue = lastValueProperties.GetProperty(fieldName); if (lastValue != null) { inObject.set_Value(fieldNum, lastValue); } //} } break; case "FIELD": // verify that field to copy exists int fieldCopy = inObject.Fields.FindField(valData as string); if (fieldCopy > -1) { //copy value only if current field is empty string currentValue = inObject.get_Value(fieldNum).ToString(); if (currentValue == "") { inObject.set_Value(fieldNum, inObject.get_Value(fieldCopy)); } } break; case "CURRENT_USER": if (lastEditorName == null) { lastEditorName = getCurrentUser(); } inObject.set_Value(fieldNum, lastEditorName); break; case "GUID": if (mode == "OnCreate") // SHould only set this once on create to give the object a unique value { object currentValue = inObject.get_Value(fieldNum); if (DBNull.Value == currentValue) // Do not overwrite if someone else has already generated { Guid g = Guid.NewGuid(); inObject.set_Value(fieldNum, g.ToString("B").ToUpper()); } } break; case "EXPRESSION": if (mode == "OnCreate") { if (inFeature != null & valData != null) { try { int calcField = inFeature.Fields.FindField(fieldName); //if (inFeature.get_Value(calcField) == null) //{ int[] fids = { inFeature.OID }; IGeoDatabaseBridge gdbBridge = new GeoDatabaseHelperClass(); IFeatureCursor fCursor = (IFeatureCursor)gdbBridge.GetFeatures((IFeatureClass)inFeature.Class, ref fids, false); ICalculator calc = new CalculatorClass(); calc.Expression = valData; calc.Field = fieldName; calc.Cursor = (ICursor)fCursor; calc.ShowErrorPrompt = false; ICalculatorCallback calculatorCallback = new CalculatorCallback(); calc.Callback = calculatorCallback; calc.Calculate(); calculatorCallback = null; //} } catch { } } } break; default: break; } } row = tabCursor.NextRow(); } if (null != tabCursor) { System.Runtime.InteropServices.Marshal.ReleaseComObject(tabCursor); } } catch (Exception ex) { _logHelper.addLogEntry(DateTime.Now.ToString(), "ERROR", "Error applying dynamic defaults.", ex.ToString()); MessageBox.Show("Error: \n" + ex.ToString()); } }