Geometry
using Autodesk.DesignScript.Geometry.Properties;
using Autodesk.DesignScript.Interfaces;
using Autodesk.DesignScript.Runtime;
using ProtoGeometryGenerator;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
namespace Autodesk.DesignScript.Geometry
{
[GenerateFromInterface(typeof(IGeometryEntity), typeof(IGeometryFactory), new Type[] {
typeof(ITransformableEntity)
})]
public abstract class Geometry : DesignScriptEntity
{
private static Dictionary<Type, Func<IGeometryEntity, Geometry>> mGeometryContructors;
private Geometry mContext;
private static Type mCachedType;
private static Func<IGeometryEntity, Geometry> mCachedConstructor;
public virtual BoundingBox BoundingBox => BoundingBox.Wrap(GeometryEntity.get_BoundingBox());
public virtual BoundingBox OrientedBoundingBox => BoundingBox.Wrap(GeometryEntity.get_OrientedBoundingBox());
public virtual CoordinateSystem ContextCoordinateSystem => CoordinateSystem.Wrap(GeometryEntity.get_ContextCoordinateSystem());
internal override IDesignScriptEntity HostImpl => base.HostImpl;
internal IGeometryEntity GeometryEntity => HostImpl as IGeometryEntity;
static Geometry()
{
mGeometryContructors = new Dictionary<Type, Func<IGeometryEntity, Geometry>>();
Type[] types = Assembly.GetExecutingAssembly().GetTypes();
Type[] array = types;
foreach (Type type in array) {
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, Geometry> contructor)
{
mGeometryContructors[hostType] = contructor;
}
internal static Geometry Wrap(IGeometryEntity host)
{
return Wrap(host, null);
}
internal static Geometry Wrap(ITransformableEntity host)
{
return Wrap(host as IGeometryEntity, null);
}
internal static Geometry Wrap(IGeometryEntity host, Geometry context)
{
if (host == null)
return null;
if (host.get_Owner() != null)
return host.get_Owner() as Geometry;
Func<IGeometryEntity, Geometry> geomConstructor = GetGeomConstructor(host);
if (geomConstructor == null)
throw new InvalidOperationException(string.Format(Resources.InvalidOperationException, ((object)host).GetType()));
return geomConstructor(host);
}
private static Func<IGeometryEntity, Geometry> GetGeomConstructor(IGeometryEntity host)
{
Type type = ((object)host).GetType();
if (type == mCachedType)
return mCachedConstructor;
Type[] interfaces = type.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, Geometry> value)) {
mCachedType = type;
mCachedConstructor = value;
return value;
}
}
return null;
}
internal Geometry(Func<IGeometryEntity> constructor)
: base((Func<IDesignScriptEntity>)constructor)
{
}
[IsVisibleInDynamoLibrary(false)]
[Obsolete("The PersistenceManager is no longer implemented in LibG. This method is deprecated and will be removed in a future version of Dynamo..")]
public static Geometry FromObject(long ptr)
{
throw new NotImplementedException("The PersistenceManager is no longer implemented in LibG. This method is deprecated and will be removed in a future version of Dynamo.");
}
protected override void Dispose(bool disposing)
{
if (disposing)
GeometryExtension.DisposeObject(ref mContext);
base.Dispose(disposing);
}
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;
}
[GenerateShadowing("SetAttributes", new Type[] {
typeof(Dictionary<string, string>)
})]
internal Geometry SetAttributes(Dictionary<string, string> attributes)
{
return Geometry.Wrap(GeometryEntity.SetAttributes(attributes));
}
[GenerateShadowing("GetAttributes", new Type[] {
})]
internal IDictionary<string, string> GetAttributes()
{
return GeometryEntity.GetAttributes();
}
[SupressImportIntoVM]
public virtual string ToJson()
{
return GeometryEntity.ToJson(DesignScriptEntity.scaleFactor);
}
public virtual Geometry Translate([Scaling()] double xTranslation = 0, [Scaling()] double yTranslation = 0, [Scaling()] double zTranslation = 0)
{
DesignScriptEntity.CheckArgsForAsmExtents(xTranslation, yTranslation, zTranslation);
return Geometry.Wrap(GeometryEntity.Translate(xTranslation / DesignScriptEntity.scaleFactor, yTranslation / DesignScriptEntity.scaleFactor, zTranslation / DesignScriptEntity.scaleFactor));
}
public virtual Geometry Translate(Vector direction)
{
return Geometry.Wrap(GeometryEntity.Translate(Vector.Unwrap(direction)));
}
public virtual Geometry Translate(Vector direction, [Scaling()] double distance)
{
DesignScriptEntity.CheckArgsForAsmExtents(distance);
return Geometry.Wrap(GeometryEntity.Translate(Vector.Unwrap(direction), distance / DesignScriptEntity.scaleFactor));
}
public virtual Geometry Transform(CoordinateSystem coordinateSystem)
{
return Geometry.Wrap(GeometryEntity.Transform(CoordinateSystem.Unwrap(coordinateSystem)));
}
public virtual Geometry Transform(CoordinateSystem targetCoordinateSystem, CoordinateSystem sourceCoordinateSystem)
{
return Geometry.Wrap(GeometryEntity.Transform(CoordinateSystem.Unwrap(targetCoordinateSystem), CoordinateSystem.Unwrap(sourceCoordinateSystem)));
}
public virtual Geometry Rotate(Point origin, Vector axis, double degrees = 0)
{
return Geometry.Wrap(GeometryEntity.Rotate(Point.Unwrap(origin), Vector.Unwrap(axis), degrees));
}
public virtual Geometry Rotate(Plane basePlane, double degrees = 0)
{
return Geometry.Wrap(GeometryEntity.Rotate(Plane.Unwrap(basePlane), degrees));
}
public virtual Geometry Mirror(Plane mirrorPlane)
{
return Geometry.Wrap(GeometryEntity.Mirror(Plane.Unwrap(mirrorPlane)));
}
public virtual Geometry Scale(double amount = 1)
{
return Geometry.Wrap(GeometryEntity.Scale(amount));
}
public virtual Geometry Scale(double xamount = 1, double yamount = 1, double zamount = 1)
{
return Geometry.Wrap(GeometryEntity.Scale(xamount, yamount, zamount));
}
public virtual Geometry Scale(Plane plane, double xamount = 1, double yamount = 1, double zamount = 1)
{
return Geometry.Wrap(GeometryEntity.Scale(Plane.Unwrap(plane), xamount, yamount, zamount));
}
public virtual Geometry Scale(Point basePoint, Point from, Point to)
{
return Geometry.Wrap(GeometryEntity.Scale(Point.Unwrap(basePoint), Point.Unwrap(from), Point.Unwrap(to)));
}
public virtual Geometry Scale1D(Point basePoint, Point from, Point to)
{
return Geometry.Wrap(GeometryEntity.Scale1D(Point.Unwrap(basePoint), Point.Unwrap(from), Point.Unwrap(to)));
}
public virtual Geometry Scale2D(Plane basePlane, Point from, Point to)
{
return Geometry.Wrap(GeometryEntity.Scale2D(Plane.Unwrap(basePlane), Point.Unwrap(from), Point.Unwrap(to)));
}
[GenerateShadowing("Clone", new Type[] {
})]
internal virtual Geometry Clone()
{
return Geometry.Wrap(GeometryEntity.Clone());
}
[Scaling()]
public virtual double DistanceTo(Geometry entity)
{
return GeometryEntity.DistanceTo(Unwrap(entity)) * DesignScriptEntity.scaleFactor;
}
public virtual Point ClosestPointTo(Geometry entity)
{
return Point.Wrap(GeometryEntity.ClosestPointTo(Unwrap(entity)));
}
public virtual Geometry[] Split(Geometry tool)
{
return Wrap(GeometryEntity.Split(Unwrap(tool)));
}
public virtual Geometry[] Trim(Geometry tool, Point pick)
{
return Wrap(GeometryEntity.Trim(Unwrap(tool), Point.Unwrap(pick)));
}
public virtual Geometry[] Explode()
{
return Track(Wrap(GeometryEntity.Explode()));
}
public virtual bool IsAlmostEqualTo(Geometry other)
{
return GeometryEntity.IsAlmostEqualTo(Unwrap(other));
}
[IsVisibleInDynamoLibrary(false)]
public virtual Geometry[] Approximate()
{
return Wrap(GeometryEntity.Approximate());
}
[AllowRankReduction]
[Obsolete("This method is deprecated and will be removed in a future version of Dynamo. Use overload that specifies mm per unit")]
[IsObsolete("geometry_importfromsat_deprecated", typeof(Resources))]
public static Geometry[] ImportFromSAT(FileInfo file)
{
string fileName = file.FullName;
IGeometryEntity[] hosts = ImportFromSAT(ref fileName, 1000);
return hosts.ToArray<Geometry, IGeometryEntity>((Geometry)null);
}
[AllowRankReduction]
[Obsolete("This method is deprecated and will be removed in a future version of Dynamo. Use overload that specifies mm per unit")]
[IsObsolete("geometry_importfromsat_deprecated", typeof(Resources))]
public static Geometry[] ImportFromSAT(string filePath)
{
IGeometryEntity[] hosts = ImportFromSAT(ref filePath, 1000);
return hosts.ToArray<Geometry, IGeometryEntity>((Geometry)null);
}
[AllowRankReduction]
[IsVisibleInDynamoLibrary(false)]
public static Geometry[] ImportFromSAT(FileInfo file, double mmPerUnit)
{
string fileName = file.FullName;
IGeometryEntity[] hosts = ImportFromSAT(ref fileName, mmPerUnit);
return hosts.ToArray<Geometry, IGeometryEntity>((Geometry)null);
}
[AllowRankReduction]
[IsVisibleInDynamoLibrary(false)]
public static Geometry[] ImportFromSAT(string filePath, double mmPerUnit)
{
IGeometryEntity[] hosts = ImportFromSAT(ref filePath, mmPerUnit);
return hosts.ToArray<Geometry, IGeometryEntity>((Geometry)null);
}
internal static IGeometryEntity[] ImportFromSAT(ref string fileName, double mmPerUnit)
{
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, mmPerUnit);
}
[AllowRankReduction]
public static Geometry[] FromSolidDef(string solidDefJson)
{
IGeometryEntity[] hosts = FromSolidDef(ref solidDefJson);
return hosts.ToArray<Geometry, IGeometryEntity>((Geometry)null);
}
internal static IGeometryEntity[] FromSolidDef(ref string solidDefJson)
{
if (string.IsNullOrWhiteSpace(solidDefJson))
throw new ArgumentNullException("solidDefJson");
return HostFactory.Factory.FromSolidDef(solidDefJson);
}
[SupressImportIntoVM]
[GenerateShadowing("FromJson", new Type[] {
typeof(string),
typeof(double)
})]
public static Geometry FromJson(string json)
{
return Geometry.Wrap(HostFactory.Factory.GeometryFromJson(json, DesignScriptEntity.scaleFactor));
}
public static string ExportToSAT(IEnumerable<Geometry> geometry, string filePath)
{
return CheckPathAndExportToSAT(geometry, filePath, false, 0);
}
[IsVisibleInDynamoLibrary(false)]
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)
{
IGeometryEntity[] hosts = HostFactory.Factory.FromNativePointer(nativePointer, DesignScriptEntity.scaleFactor);
return hosts.ToArray<Geometry, IGeometryEntity>((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);
}
object factory = (object)HostFactory.Factory;
object[] array = list.ToArray();
return factory.ToNativePointer(array, 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;
string directoryName = Path.GetDirectoryName(text);
filePath = Path.Combine(directoryName, filePath);
}
object[] array;
if (!useUnits) {
object factory = (object)HostFactory.Factory;
string text2 = filePath;
array = list.ToArray();
return factory.SaveSAT(text2, array, DesignScriptEntity.scaleFactor);
}
object factory2 = (object)HostFactory.Factory;
string text3 = filePath;
array = list.ToArray();
return factory2.SaveSAT(text3, array, unitsMM, DesignScriptEntity.scaleFactor);
}
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);
}
object factory = (object)HostFactory.Factory;
object[] array = list.ToArray();
return factory.SerializeAsSAB(array, DesignScriptEntity.scaleFactor);
}
[AllowRankReduction]
[Obsolete("This method is deprecated and will be removed in a future version of Dynamo. Please use overload that allows passing mmPerUnit")]
[IsObsolete("geometry_deserializefromsab_deprecated", typeof(Resources))]
public static Geometry[] DeserializeFromSAB(byte[] buffer)
{
IGeometryEntity[] hosts = DeserializeFromSABInternal(buffer, 1000);
return hosts.ToArray<Geometry, IGeometryEntity>((Geometry)null);
}
[AllowRankReduction]
[IsVisibleInDynamoLibrary(false)]
public static Geometry[] DeserializeFromSAB(byte[] buffer, double mmPerUnit)
{
IGeometryEntity[] hosts = DeserializeFromSABInternal(buffer, mmPerUnit);
return hosts.ToArray<Geometry, IGeometryEntity>((Geometry)null);
}
internal static IGeometryEntity[] DeserializeFromSABInternal(byte[] buffer, double mmPerUnit)
{
return HostFactory.Factory.DeserializeFromSAB(buffer, DesignScriptEntity.scaleFactor, mmPerUnit);
}
[SupressImportIntoVM]
[Obsolete("The PersistenceManager is no longer implemented in LibG. This method is deprecated and will be removed in a future version of Dynamo.")]
public static void UpdateDisplay()
{
throw new NotImplementedException("This method is no longer implementation. This method is deprecated and will be removed in a future version of Dynamo.");
}
protected override int ComputeHashCode()
{
return base.ComputeHashCode();
}
internal static string GetFullPath(string fileName)
{
if (Path.IsPathRooted(fileName))
return fileName;
IConfiguration val = Application.Instance.Session as IConfiguration;
return Path.Combine(Path.GetDirectoryName(val.get_RootModulePath()), fileName);
}
internal Geometry(IGeometryEntity host)
: base(host)
{
}
public bool DoesIntersect(Geometry entity)
{
return GeometryEntity.DoesIntersect(Unwrap(entity));
}
public Geometry[] Intersect(Geometry other)
{
return Wrap(GeometryEntity.Intersect(Unwrap(other)));
}
public Geometry[] IntersectAll(IEnumerable<Geometry> entity)
{
return Wrap(GeometryEntity.IntersectAll(Unwrap(entity)));
}
public string ToSolidDef()
{
return GeometryEntity.ToSolidDef();
}
public Geometry[] ProjectPointsOnto(IEnumerable<Point> points, Vector projectionDirection)
{
return Wrap(GeometryEntity.ProjectPointsOnto(Point.Unwrap(points), Vector.Unwrap(projectionDirection)));
}
internal static Geometry[] Wrap(IGeometryEntity[] hosts)
{
return hosts.Select(Wrap).ToArray();
}
internal static Geometry[][] Wrap(IGeometryEntity[][] hosts)
{
return hosts.Select(Wrap).ToArray();
}
internal static IGeometryEntity Unwrap(Geometry o)
{
return o.GeometryEntity;
}
internal static IGeometryEntity[] Unwrap(IEnumerable<Geometry> o)
{
return o.Select(Unwrap).ToArray();
}
internal static IGeometryEntity[] Unwrap(Geometry[] o)
{
return o.Select(Unwrap).ToArray();
}
internal static IGeometryEntity[][] Unwrap(Geometry[][] o)
{
return o.Select(Unwrap).ToArray();
}
public override string ToString()
{
StringBuilder builder = DesignScriptEntity.GetBuilder();
AppendTo(builder);
return builder.ToString();
}
internal override void AppendTo(StringBuilder builder)
{
builder.Add("Geometry(ContextCoordinateSystem = ", ContextCoordinateSystem).Add(", OrientedBoundingBox = ", OrientedBoundingBox).Append(")");
}
}
}