ArcFM Engine Developer Guide
RatedKVARule.cs Code

Resource Center Home

Validation Rule Developer Sample (C#)

RatedKVARule.cs

 

Copy Code
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Esri.ArcGIS.Geodatabase;
using Esri.ArcGIS.esriSystem;
using Miner.Interop;
using Miner.ComCategories;
namespace Miner.Samples.ValidationRules
{
 [ComponentCategory(ComCategory.MMValidationRules)]
 [ComVisible(true)]
 [Guid("04B41DC9-4122-427c-9E58-93A759219C5D")]
 public class RatedKVARule : IMMExtObject, IMMValidationRule
 {
  #region Private fields
  private const string _transformer = "Transformer";
  private const string _transUnit = "TransformerUnit";
  private const string _kvaFieldModelName = "RatedKVA";
//  private Resourcer _resourcer;
  private IMMModelNameManager _modelNameManager;
  #endregion
  #region Constructor
  public RatedKVARule()
  {
   
  }
  #endregion
  #region IMMExtObject Members
  public stdole.IPictureDisp Bitmap
  {
   get
   {
    return null;
   }
  }
  public bool get_Enabled(ref object pvarValues)
  {
   bool enabled = false;
   try
   {
    IObjectClass objClass = pvarValues as IObjectClass;
    if(pvarValues == null || objClass == null)
    {
     return enabled;
    }
    if(_modelNameManager == null)
    {
     _modelNameManager = QASampleUtilities.GetIMMModelNameManager();
     if(_modelNameManager != null)
     {
      enabled = _modelNameManager.ContainsClassModelName(
       objClass, _transformer);
     }
    }
   }
   catch(Exception ex)
   {
    System.Windows.Forms.MessageBox.Show(ex.Message,
     this.GetType() + ".IMMExtObject.Enabled",
     System.Windows.Forms.MessageBoxButtons.OK);
   }
   return enabled;
  }
  public string Name
  {
   get
   {
    return "Sample Rated KVA on Transformer (C#)";
   }
  }
  #endregion
  
  #region IMMValidationRule Members
  public ID8List IsValid(IRow pRow)
  {
   ID8List d8List = null;
   try
   {
    if(pRow == null)
    {
     return d8List;
    }
    if(_modelNameManager == null)
    {
     _modelNameManager = QASampleUtilities.GetIMMModelNameManager();
    }
    d8List = new D8ListClass();
    
    IObject obj = pRow as IObject;
    if(obj != null)
    {
     IObjectClass objClass = obj.Class;
     if(! _modelNameManager.ContainsClassModelName(objClass, _transformer))
     {
      return d8List;
     }
     object objKVA = QASampleUtilities.GetRowValueByFieldModelName(
      obj, _kvaFieldModelName, _modelNameManager);
     if(IsValidKVAValue(objKVA))
     {
      IRelationshipClass relationShipCls
       = QASampleUtilities.GetRelatedClassByModelName(objClass, 
       esriRelRole.esriRelRoleOrigin, _transUnit);
      if(relationShipCls != null)
      {
       ISet objSet = 
        relationShipCls.GetObjectsRelatedToObject(obj);
       if(objSet == null)
       {
        return d8List;
       }
       IObject objRelation;
       object objUnitKVA;
       double totalUnitKVA = 0.0;
       objSet.Reset();
       do
       {
        objRelation = objSet.Next() as IObject;
        if(objRelation == null)
        {
         break;
        }
        objUnitKVA = 
         QASampleUtilities.GetRowValueByFieldModelName(
         objRelation, _kvaFieldModelName, 
         _modelNameManager);
        if(IsValidKVAValue(objUnitKVA))
        {
         totalUnitKVA += ConvertDB2ActualKVA(Convert.ToDouble(objUnitKVA));
        }
       }while(true);
       if(totalUnitKVA > 0)
       {
        if(totalUnitKVA != (double) objKVA)
        {
         string s = "value of Rated KVA of transformer (Rated JVA: " + 
          ((double) objKVA).ToString() + ") is not equal to the total " +
          "Rated KVA of related Transformer Units (Rated KVA: " +
          totalUnitKVA.ToString() + ".";
         QASampleUtilities.CreateNewError(ref d8List, 8, s, 0);
        }
       }
      }
     }
    }
   }
   catch(Exception ex)
   {
    MessageBox.Show(ex.Message,
     GetType() + ".IMMValidationRule.IsValid",
     MessageBoxButtons.OK);
   }
   return d8List;;
  }
  #endregion
  #region Private methods
  private static double ConvertDB2ActualKVA(double databaseKVA)
  {
   double d = databaseKVA;
   if(databaseKVA == 38)
   {
    d = 37.5;
   }
   
   return d;
  }
  private static bool IsValidKVAValue(object objKVA)
  {
   bool bValid = false;
   try
   {
    if(objKVA != null)
    {
     if(IsNumeric(objKVA))
     {
//      bValid = ((double) objKVA > 0);
      bValid = (double.Parse(objKVA.ToString()) > 0);
     }
    }
   }
   catch(Exception ex)
   {
    MessageBox.Show(ex.Message,
     "ValidationRule.IsValidKVAValue",
     MessageBoxButtons.OK);
   }
   return bValid;
  }
  private static bool IsNumeric(object value)
  {
   try
   { 
    Convert.ToDouble(value.ToString());
    return true; 
   } 
   catch (FormatException) 
   { 
    return false;
   }
  }
  #endregion
 }
}

 

 


Send Comment to ArcFMdocumentation@schneider-electric.com