DesignScriptEntity
using Autodesk.DesignScript.Geometry.Core;
using Autodesk.DesignScript.Geometry.Properties;
using Autodesk.DesignScript.Interfaces;
using Autodesk.DesignScript.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Autodesk.DesignScript.Geometry
{
[SupressImportIntoVM]
public abstract class DesignScriptEntity : IDisposable, IGraphicItem
{
private IDesignScriptEntity mEntity;
protected Func<IDesignScriptEntity> mConstructor;
private int mRefCount = 1;
[SupressImportIntoVM]
public EntityTags Tags = new EntityTags(null, null);
private int? mHashCode;
internal bool IsDisposed {
get {
if (mEntity != null)
return mRefCount <= 0;
return true;
}
}
internal virtual IDesignScriptEntity HostImpl {
get {
if (mEntity == null && mConstructor != null)
InitEntity(mConstructor());
return mEntity;
}
}
internal T Track<T>(T childGeometry) where T : DesignScriptEntity
{
childGeometry.Tags.Parent = this;
return childGeometry;
}
internal IEnumerable<T> Track<T>(IEnumerable<T> childGeometries) where T : DesignScriptEntity
{
return from x in childGeometries
select Track(x);
}
internal T[] Track<T>(T[] childGeometries) where T : DesignScriptEntity
{
return (from x in childGeometries
select Track(x)).ToArray();
}
internal DesignScriptEntity(Func<IDesignScriptEntity> constructor)
{
mConstructor = constructor;
}
internal DesignScriptEntity(IDesignScriptEntity entity, bool persist = true)
{
InitEntity(entity);
}
internal void InitEntity(IDesignScriptEntity entity)
{
if (entity == null)
throw new ArgumentNullException("host", Resources.NullIDesignScriptEntityException);
if (entity.Owner != null)
throw new ArgumentException(Resources.OwnerExistingException, "host");
mEntity = entity;
mEntity.SetOwner(this);
}
public void Tessellate(IRenderPackage package, TessellationParameters parameters)
{
(mEntity as IGraphicItem)?.Tessellate(package, parameters);
}
public void Dispose()
{
if (Application.Instance.IsExecuting)
DisposeDisplayable();
ReleaseEntity();
}
protected virtual void DisposeDisplayable()
{
}
~DesignScriptEntity()
{
Dispose(true);
}
internal void RetainEntity()
{
mRefCount++;
}
internal void ReleaseEntity()
{
bool flag = mRefCount == 1;
mRefCount--;
if (flag)
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (disposing) {
if (mRefCount <= 0) {
mRefCount = 0;
if (mEntity != null) {
mEntity.Dispose();
mEntity = null;
GC.SuppressFinalize(this);
}
}
} else {
mEntity = null;
mConstructor = null;
}
}
public override string ToString()
{
return mEntity.ToString();
}
public sealed override int GetHashCode()
{
if (!mHashCode.HasValue)
mHashCode = ComputeHashCode();
return mHashCode.Value;
}
protected virtual int ComputeHashCode()
{
int num = (mEntity != null) ? mEntity.GetHashCode() : 0;
if (num != 0)
return num;
return base.GetHashCode();
}
public sealed override bool Equals(object obj)
{
if (object.ReferenceEquals(this, obj))
return true;
DesignScriptEntity designScriptEntity = obj as DesignScriptEntity;
if (designScriptEntity == null)
return false;
return Equals(designScriptEntity);
}
protected virtual bool Equals(DesignScriptEntity dsentity)
{
if (mEntity != null && object.ReferenceEquals(mEntity, dsentity.mEntity))
return true;
return GetHashCode() == dsentity.GetHashCode();
}
}
}