Geometry
using Autodesk.DesignScript.Geometry.Properties;
using Autodesk.DesignScript.Interfaces;
using Autodesk.DesignScript.Runtime;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
namespace Autodesk.DesignScript.Geometry
{
public abstract class Geometry : DesignScriptEntity
{
private static Dictionary<Type, Func<IGeometryEntity, bool, Geometry>> mGeometryContructors;
private Geometry mContext;
internal IPersistentObject mPersistent;
public BoundingBox BoundingBox => BoundingBox.Wrap(GeometryEntity.get_BoundingBox(), true);
public CoordinateSystem ContextCoordinateSystem => CoordinateSystem.Wrap(GeometryEntity.get_ContextCoordinateSystem(), true);
internal override IDesignScriptEntity HostImpl {
get {
if (mPersistent != null)
return mPersistent.get_Geometry();
return base.HostImpl;
}
}
internal IGeometryEntity GeometryEntity => HostImpl as IGeometryEntity;
internal bool IsPersistent => mPersistent != null;
static Geometry()
{
mGeometryContructors = new Dictionary<Type, Func<IGeometryEntity, bool, Geometry>>();
Type[] types = Assembly.GetExecutingAssembly().GetTypes();
foreach (Type type in types) {
if (type.IsClass && !type.IsAbstract && typeof(Geometry).IsAssignableFrom(type)) {
MethodInfo method = type.GetMethod("InitType", BindingFlags.Static | BindingFlags.NonPublic);
if ((MethodInfo)null != method)
method.Invoke(null, null);
}
}
}
internal static void RegisterHostType(Type hostType, Func<IGeometryEntity, bool, Geometry> contructor)
{
mGeometryContructors[hostType] = contructor;
}
internal static Geometry Wrap(IGeometryEntity host, bool persist = false, Geometry context = null)
{
if (host == null)
return null;
if (host.get_Owner() != null)
return host.get_Owner() as Geometry;
Func<IGeometryEntity, bool, Geometry> geomConstructor = GetGeomConstructor(host);
if (geomConstructor == null)
throw new InvalidOperationException(string.Format(Resources.InvalidOperationException, ((object)host).GetType()));
return geomConstructor(host, persist);
}
internal static Geometry[] Wrap(IGeometryEntity[] geometries)
{
List<Geometry> list = new List<Geometry>();
foreach (IGeometryEntity host in geometries) {
list.Add(Wrap(host, false, null));
}
return list.ToArray();
}
internal static IGeometryEntity[] Unwrap(Geometry[] geometries)
{
List<IGeometryEntity> list = new List<IGeometryEntity>();
foreach (Geometry geometry in geometries) {
list.Add(geometry.GeometryEntity);
}
return list.ToArray();
}
internal static IGeometryEntity[] Unwrap(IEnumerable<Geometry> o)
{
return (from x in o
select Unwrap(x)).ToArray();
}
internal static IGeometryEntity Unwrap(Geometry geom)
{
return geom.GeometryEntity;
}
private static Func<IGeometryEntity, bool, Geometry> GetGeomConstructor(IGeometryEntity host)
{
Type[] interfaces = ((object)host).GetType().GetInterfaces();
interfaces = interfaces.Except(interfaces.SelectMany((Type t) => t.GetInterfaces())).ToArray();
Type[] array = interfaces;
foreach (Type key in array) {
if (mGeometryContructors.TryGetValue(key, out Func<IGeometryEntity, bool, Geometry> value))
return value;
}
return null;
}
internal Geometry(IGeometryEntity host, bool persist)
: base(host, true)
{
InitGeometry(persist);
}
internal Geometry(Func<IGeometryEntity> constructor, bool persist)
: base((Func<IDesignScriptEntity>)constructor)
{
InitGeometry(persist);
}
[IsVisibleInDynamoLibrary(false)]
public static Geometry FromObject(long ptr)
{
IPersistentObject val = HostFactory.PersistenceManager.FromObject(ptr);
Geometry geometry = Wrap(val.get_Geometry(), false, null);
geometry.mPersistent = val;
return geometry;
}
private void InitGeometry(bool persist)
{
if (persist)
Persist();
}
internal IPersistentObject Persist()
{
if (mPersistent != null)
return mPersistent;
if ((object)HostFactory.PersistenceManager == null)
return null;
IGeometryEntity val = HostImpl as IGeometryEntity;
if (val == null)
return null;
DisposeDisplay();
mPersistent = HostFactory.PersistenceManager.Persist(val);
return mPersistent;
}
protected override void DisposeDisplayable()
{
DisposeDisplay();
if (mPersistent != null)
mPersistent.Erase();
base.DisposeDisplayable();
}
protected override void Dispose(bool disposing)
{
if (disposing) {
if (mPersistent != null && (object)mPersistent != (object)base.HostImpl)
((IDisposable)mPersistent).Dispose();
GeometryExtension.DisposeObject(ref mContext);
}
mPersistent = null;
base.Dispose(disposing);
}
private void DisposeDisplay()
{
}
internal static int GetIndexOfNearestGeometry(IGeometryEntity[] entities, IPointEntity point)
{
double num = entities[0].DistanceTo(point);
int result = 0;
for (int i = 1; i < entities.Length; i++) {
double num2 = entities[i].DistanceTo(point);
if (num2 < num) {
num = num2;
result = i;
}
}
return result;
}
public Geometry Translate(double xTranslation = 0, double yTranslation = 0, double zTranslation = 0)
{
xTranslation /= DesignScriptEntity.scaleFactor;
yTranslation /= DesignScriptEntity.scaleFactor;
zTranslation /= DesignScriptEntity.scaleFactor;
return Wrap(GeometryEntity.Translate(xTranslation, yTranslation, zTranslation) as IGeometryEntity, true, null);
}
public Geometry Translate(Vector direction)
{
return Wrap(GeometryEntity.Translate(direction.VectorEntity) as IGeometryEntity, true, null);
}
public Geometry Translate(Vector direction, double distance)
{
distance /= DesignScriptEntity.scaleFactor;
return Wrap(GeometryEntity.Translate(direction.VectorEntity, distance) as IGeometryEntity, true, null);
}
public Geometry Transform(CoordinateSystem cs)
{
return Wrap(GeometryEntity.Transform(cs.CoordinateSystemEntity) as IGeometryEntity, true, null);
}
public Geometry Transform(CoordinateSystem fromCoordinateSystem, CoordinateSystem contextCoordinateSystem)
{
return Wrap(GeometryEntity.TransformFromTo(fromCoordinateSystem.CoordinateSystemEntity, contextCoordinateSystem.CoordinateSystemEntity) as IGeometryEntity, true, null);
}
public Geometry Rotate(Point origin, Vector axis, double degrees = 0)
{
return Wrap(GeometryEntity.Rotate(origin.PointEntity, axis.VectorEntity, degrees) as IGeometryEntity, true, null);
}
public Geometry Rotate(Plane basePlane, double degrees = 0)
{
return Wrap(GeometryEntity.Rotate(basePlane.PlaneEntity, degrees) as IGeometryEntity, true, null);
}
public Geometry Mirror(Plane mirrorPlane)
{
return Wrap(GeometryEntity.Mirror(mirrorPlane.PlaneEntity) as IGeometryEntity, true, null);
}
public Geometry Scale(double amount = 1)
{
return Wrap(GeometryEntity.Scale(amount) as IGeometryEntity, true, null);
}
public Geometry Scale(double xamount = 1, double yamount = 1, double zamount = 1)
{
return Wrap(GeometryEntity.Scale(xamount, yamount, zamount) as IGeometryEntity, true, null);
}
public Geometry Scale(Plane plane, double xamount = 1, double yamount = 1, double zamount = 1)
{
return Wrap(GeometryEntity.Scale(Plane.Unwrap(plane), xamount, yamount, zamount) as IGeometryEntity, true, null);
}
public Geometry Scale(Point basePoint, Point from, Point to)
{
return Wrap(GeometryEntity.Scale(basePoint.PointEntity, from.PointEntity, to.PointEntity) as IGeometryEntity, true, null);
}
public Geometry Scale1D(Point basePoint, Point from, Point to)
{
return Wrap(GeometryEntity.Scale1D(basePoint.PointEntity, from.PointEntity, to.PointEntity) as IGeometryEntity, true, null);
}
public Geometry Scale2D(Plane basePlane, Point from, Point to)
{
return Wrap(GeometryEntity.Scale2D(basePlane.PlaneEntity, from.PointEntity, to.PointEntity) as IGeometryEntity, true, null);
}
public double DistanceTo(Geometry other)
{
return GeometryEntity.DistanceTo(other.GeometryEntity) * DesignScriptEntity.scaleFactor;
}
public Point ClosestPointTo(Geometry other)
{
return Point.Wrap(GeometryEntity.ClosestPointTo(other.GeometryEntity), true);
}
public bool DoesIntersect(Geometry other)
{
return GeometryEntity.DoesIntersect(other.GeometryEntity);
}
public Geometry[] Intersect(Geometry other)
{
return Wrap(GeometryEntity.Intersect(other.GeometryEntity));
}
public Geometry[] IntersectAll(IEnumerable<Geometry> others)
{
return Wrap(GeometryEntity.IntersectAll(Unwrap(others)));
}
public Geometry[] Split(Geometry other)
{
return Wrap(GeometryEntity.Split(other.GeometryEntity));
}
public Geometry[] Trim(Geometry other, Point pick)
{
return Wrap(GeometryEntity.Trim(other.GeometryEntity, pick.PointEntity));
}
public Geometry[] Explode()
{
return Track(Wrap(GeometryEntity.Explode())).ToArray();
}
public bool IsAlmostEqualTo(Geometry other)
{
return GeometryEntity.IsAlmostEqualTo(other.GeometryEntity);
}
public string ToSolidDef()
{
return GeometryEntity.ToSolidDef();
}
[AllowRankReduction]
public static Geometry[] ImportFromSAT(FileInfo file)
{
string fileName = file.FullName;
return ImportFromSAT(ref fileName).ToArray<Geometry, IGeometryEntity>(true, (Geometry)null);
}
[AllowRankReduction]
public static Geometry[] ImportFromSAT(string filePath)
{
return ImportFromSAT(ref filePath).ToArray<Geometry, IGeometryEntity>(true, (Geometry)null);
}
internal static IGeometryEntity[] ImportFromSAT(ref string fileName)
{
if (string.IsNullOrWhiteSpace(fileName))
throw new ArgumentNullException("fileName");
fileName = GeometryExtension.LocateFile(fileName);
if (!File.Exists(fileName))
throw new ArgumentException(string.Format(Resources.FileNotFound, fileName), "fileName");
return HostFactory.Factory.LoadSAT(fileName, DesignScriptEntity.scaleFactor);
}
[AllowRankReduction]
public static Geometry[] FromSolidDef(string solidDefJson)
{
return FromSolidDef(ref solidDefJson).ToArray<Geometry, IGeometryEntity>(true, (Geometry)null);
}
internal static IGeometryEntity[] FromSolidDef(ref string solidDefJson)
{
if (string.IsNullOrWhiteSpace(solidDefJson))
throw new ArgumentNullException("solidDefJson");
return HostFactory.Factory.FromSolidDef(solidDefJson);
}
[SupressImportIntoVM]
[Obsolete("This method is deprecated, use ExportToSAT(IEnumerable<Geometry> geometry, string filePath) instead")]
public string ExportToSAT(string filePath)
{
return ExportToSAT(new List<Geometry> {
this
}.ToArray(), filePath);
}
[SupressImportIntoVM]
[Obsolete("This method is deprecated, use ExportToSAT UI node instead")]
public string ExportToSAT(string filePath, double unitsMM)
{
return ExportToSAT(new List<Geometry> {
this
}.ToArray(), filePath, unitsMM);
}
public static string ExportToSAT(IEnumerable<Geometry> geometry, string filePath)
{
return CheckPathAndExportToSAT(geometry, filePath, false, 0);
}
[IsObsolete("This method is deprecated, use ExportToSAT UI node instead")]
public static string ExportToSAT(IEnumerable<Geometry> geometry, string filePath, double unitsMM)
{
return CheckPathAndExportToSAT(geometry, filePath, true, unitsMM);
}
[IsVisibleInDynamoLibrary(false)]
public static Geometry[] FromNativePointer(IntPtr nativePointer)
{
return HostFactory.Factory.FromNativePointer(nativePointer, DesignScriptEntity.scaleFactor).ToArray<Geometry, IGeometryEntity>(true, (Geometry)null);
}
[IsVisibleInDynamoLibrary(false)]
public static IntPtr[] ToNativePointer(IEnumerable<Geometry> geometry)
{
List<IGeometryEntity> list = new List<IGeometryEntity>();
foreach (Geometry item in geometry) {
IGeometryEntity geometryEntity = item.GeometryEntity;
if (geometryEntity != null)
list.Add(geometryEntity);
}
return HostFactory.Factory.ToNativePointer((object[])list.ToArray(), DesignScriptEntity.scaleFactor);
}
private static string CheckPathAndExportToSAT(IEnumerable<Geometry> geometry, string filePath, bool useUnits, double unitsMM = 0)
{
List<IGeometryEntity> list = new List<IGeometryEntity>();
foreach (Geometry item in geometry) {
IGeometryEntity geometryEntity = item.GeometryEntity;
if (geometryEntity != null)
list.Add(geometryEntity);
}
if (list.Count == 0)
throw new Exception(Resources.ConversionException);
if (!filePath.EndsWith(".sat"))
filePath += ".sat";
if (!Path.IsPathRooted(filePath)) {
string text = GeometrySettings.RootModulePath;
if (string.IsNullOrEmpty(text))
text = AppDomain.CurrentDomain.BaseDirectory;
filePath = Path.Combine(Path.GetDirectoryName(text), filePath);
}
if (!useUnits)
return HostFactory.Factory.SaveSAT(filePath, (object[])list.ToArray(), DesignScriptEntity.scaleFactor);
return HostFactory.Factory.SaveSAT(filePath, (object[])list.ToArray(), unitsMM, DesignScriptEntity.scaleFactor);
}
[SupressImportIntoVM]
[Obsolete("This method is deprecated, use SerializeAsSAB(IEnumerable<Geometry> geometry")]
public byte[] SerializeAsSAB()
{
return SerializeAsSAB(new List<Geometry> {
this
}.ToArray());
}
public static byte[] SerializeAsSAB(IEnumerable<Geometry> geometry)
{
List<IGeometryEntity> list = new List<IGeometryEntity>();
foreach (Geometry item in geometry) {
IGeometryEntity geometryEntity = item.GeometryEntity;
if (geometryEntity != null)
list.Add(geometryEntity);
}
return HostFactory.Factory.SerializeAsSAB((object[])list.ToArray(), DesignScriptEntity.scaleFactor);
}
[AllowRankReduction]
public static Geometry[] DeserializeFromSAB(byte[] buffer)
{
return DeserializeFromSABInternal(buffer).ToArray<Geometry, IGeometryEntity>(true, (Geometry)null);
}
internal static IGeometryEntity[] DeserializeFromSABInternal(byte[] buffer)
{
return HostFactory.Factory.DeserializeFromSAB(buffer, DesignScriptEntity.scaleFactor);
}
[SupressImportIntoVM]
public static void UpdateDisplay()
{
HostFactory.PersistenceManager.UpdateDisplay();
}
protected override int ComputeHashCode()
{
int num = (mPersistent != null) ? ((object)mPersistent).GetHashCode() : 0;
if (num != 0)
return num;
return base.ComputeHashCode();
}
internal static string GetFullPath(string fileName)
{
if (Path.IsPathRooted(fileName))
return fileName;
return Path.Combine(Path.GetDirectoryName((Application.Instance.Session as IConfiguration).get_RootModulePath()), fileName);
}
}
}