Method GetCompareHash
- Namespace
- VectorDraw.Professional.vdPrimaries
- Assembly
- VectorDraw.Professional.dll
GetCompareHash()
Called when this object is going to be compare with an other object by Compare(vdDocument)
public virtual int GetCompareHash()
Returns
- int
Returns a unique 32 bit integer value according to the object type and its properties values.
if it returns 0 the object is not comparable
Examples
Remove duplicated entities from a document by using GetCompareHash method
doc.Open("C:\mydrawing.vdcl");
System.Collections.Generic.Dictionary<int, vdArray<vdFigure>> hashtbl = new System.Collections.Generic.Dictionary<int, vdArray<vdFigure>>();
foreach (vdFigure item in doc.Model.Entities)
{
int hash = item.GetCompareHash();
if(hash == 0) continue;// if the object is not comparable continue
if (!hashtbl.ContainsKey(hash)) hashtbl.Add(hash, new vdArray<vdFigure>());
hashtbl[hash].AddItem(item);
}
int deletedCount = 0;
foreach (var item in hashtbl)
{
//keep the first remove the others
for (int i = 1; i <item.Value.Count; i++)
{
item.Value[i].Deleted = true;
deletedCount++;
}
}
doc.Prompt("\r\nDeleted " + deletedCount + " duplicate entities"); doc.Prompt(null);
doc.ZoomExtents();
doc.Redraw(false);
Example override GetCompareHash method in order your custom object to be comparable
public class CustomObject : vdFigure , IvdProxyFigure
{
gPoint mOrigin = new gPoint();
Vector mNormal = new Vector();
double mRotation = 0;//in radians
int mIntvalue = 0;
string mDisplayText = "";
gPoints mpoints = new gPoints();
vdEntities mSomeEntities = new vdEntities();
Int32Array mIntCollection = new Int32Array();
DoubleArray mDoubleCollection = new DoubleArray();
public override int GetCompareHash()
{
unchecked
{
int hash = 17;//start with this line
hash = hash * 23 + GetType().GetHashCode();//always put this at the start and follow the object properties
int hash2 = mpoints.GetHashCode();
if (hash2 == 0) return 0;//always check the collections depend on the vdDocument.FileProperties.CompareDocumentSettings.CollectionLimit
hash = hash * 23 + hash2;
hash2 = mSomeEntities.GetHashCode();
if (hash2 == 0) return 0;//always check the collections depend on the vdDocument.FileProperties.CompareDocumentSettings.CollectionLimit
hash = hash * 23 + hash2;
hash2 = mIntCollection.GetHashCode();
if (hash2 == 0) return 0;//always check the collections depend on the vdDocument.FileProperties.CompareDocumentSettings.CollectionLimit
hash = hash * 23 + hash2;
hash2 = mDoubleCollection.GetHashCode();
if (hash2 == 0) return 0;//always check the collections depend on the vdDocument.FileProperties.CompareDocumentSettings.CollectionLimit
hash = hash * 23 + hash2;
hash = hash * 23 + Globals.GetCompareHash(mOrigin);
hash = hash * 23 + Globals.GetCompareHash(mNormal);
hash = hash * 23 + Globals.GetCompareHashAngle(mRotation);//use GetCompareHashAngle for rotation in radians
hash = hash * 23 + mIntvalue.GetHashCode();
hash = hash * 23 + mDisplayText.GetHashCode();
return hash;
}
}
}
Remarks
- Override this method to your custom object and return a unique 32 bit integer value according to the object type and its properties values
- The default implementation returns 0 so if you want your custom object to be comparable you should override this method and return a unique value according to the object type and its properties values.
- Default objects implemantations of this method return a unique value according to the object type and its geometry properties values so they are comparable by default.
Layer colors linetypes penstyles and other properties that do not affect the geometry are not included in the default implementation of this method in order to make it more possible to be used for comparison of the geometry of the objects. - if it returns 0 the object is not comparable and will be considered different from any other object even if they are identical.