internal static DSGeometry ToGeometry(IGeometryEntity host, bool persist = false, DSGeometry context = null) { if (host == null) { return(null); } if (host.Owner != null) { return(host.Owner as DSGeometry); } Func <IGeometryEntity, bool, DSGeometry> constructor = GetGeomConstructor(host); if (null == constructor) { throw new InvalidOperationException(string.Format("Can't locate DSGeometry constructor for type: {0}.", host.GetType())); } DSGeometry geom = constructor(host, persist); if (null != context) { geom.Context = context; } return(geom); }
protected override DSGeometry GetGeometryCore(out bool autodispose) { ISolidEntity solid = ShellEntity.GetSolidGeometry(); autodispose = true; return(DSGeometry.ToGeometry(solid)); }
protected static void Hide(DSGeometry geometry) { if (null != geometry) { geometry.Display.SetVisibility(false); } }
internal static GEOM[] ToArray <GEOM, ENTITY>(this ENTITY[] hosts, bool persist = true, DSGeometry context = null) where GEOM : DSGeometry where ENTITY : IGeometryEntity { if (null == hosts) { return(null); } List <GEOM> objects = new List <GEOM>(); foreach (var item in hosts) { if (null == item) { continue; } GEOM obj = DSGeometry.ToGeometry(item, persist, context) as GEOM; if (obj == null) { continue; } objects.Add(obj); } return(objects.ToArray()); }
protected override DSGeometry GetGeometryCore(out bool autodispose) { IGeometryEntity entity = MeshEntity.Geometry; autodispose = true; return(DSGeometry.ToGeometry(entity, true)); }
/// <summary> /// Exports the geometries of the block to the outside file. /// This is similar to AutoCAD's wblock operation. /// </summary> /// <param name="filePath">The outside file path</param> /// <returns>Returns true if the export operation is successful</returns> public bool ExportGeometry(string filePath) { filePath = DSGeometry.GetFullPath(filePath); string kMethodName = "DSBlock.ExportGeometry"; if (string.IsNullOrEmpty(filePath)) { throw new ArgumentException(string.Format(Properties.Resources.InvalidInput, filePath, kMethodName), "filePath"); } if (string.IsNullOrEmpty(Name)) { throw new ArgumentException(string.Format(Properties.Resources.InvalidInput, Name, kMethodName), "sourceBlockName"); } IBlockHelper helper = HostFactory.Factory.GetBlockHelper(); if (null == helper) { throw new InvalidOperationException(string.Format(Properties.Resources.OperationFailed, kMethodName)); } return(helper.ExportBlock(filePath, Name)); }
/// <summary> /// /// </summary> /// <param name="contextGeometries"></param> /// <param name="sortAscending"></param> /// <returns></returns> public Autodesk.DesignScript.Geometry.DSGeometry[] SortByDistance(DSGeometry[] contextGeometries, bool sortAscending) { if (contextGeometries == null) { throw new System.ArgumentNullException("contextGeometries"); } else if (contextGeometries.Length == 0) { throw new System.ArgumentException(string.Format(Properties.Resources.IsZero, "number of context geometries"), "contextGeometries"); } // insert all the geometries with their distance from this point in this sorted dictionary // //SortedDictionary<double, Geometry> geomsWithDistance = new SortedDictionary<double, Geometry>(); List <KeyValuePair <double, DSGeometry> > geomsWithDistance = new List <KeyValuePair <double, DSGeometry> >(); foreach (var geom in contextGeometries) { geomsWithDistance.Add(new KeyValuePair <double, DSGeometry>(geom.GeomEntity.DistanceTo(PointEntity), geom)); } DoubleComparer comparer = new DoubleComparer(sortAscending); geomsWithDistance.Sort((KeyValuePair <double, DSGeometry> x, KeyValuePair <double, DSGeometry> y) => comparer.Compare(x.Key, y.Key)); DSGeometry[] sortedList = new DSGeometry[geomsWithDistance.Count]; int i = 0; foreach (var item in geomsWithDistance) { sortedList[i++] = item.Value; } return(sortedList); }
/// <summary> /// Instantiates the specified block by name with the orientation /// specified by the coordinate system into the target file. /// If the block does not exist in the target file, the block will be exported to the /// target file first. /// If the block already exists in the target file, the old block will be replaced with /// the new one. /// </summary> /// <param name="contextCoordinateSystem"> /// Specifies the orientation of the block. Origin is the placement point. /// This coordinate system must be orthogonal, can be non-uniformly scaled /// </param> /// <param name="targetFileName">the outside file name</param> /// <returns>If the insertion succeeds, returns true</returns> public bool ByCoordinateSystem(DSCoordinateSystem contextCoordinateSystem, string targetFileName) { string kMethodName = "DSBlock.ByCoordinateSystem "; if (null == contextCoordinateSystem) { throw new ArgumentNullException("contextCoordinateSystem"); } if (contextCoordinateSystem.IsSheared) { throw new ArgumentException(string.Format(Properties.Resources.Sheared, "contextCoordinateSystem"), "contextCoordinateSystem"); } if (string.IsNullOrEmpty(targetFileName)) { throw new ArgumentException(string.Format(Properties.Resources.InvalidInput, Name, kMethodName), "blockName"); } targetFileName = DSGeometry.GetFullPath(targetFileName); IBlockHelper helper = HostFactory.Factory.GetBlockHelper(); if (null == helper) { throw new InvalidOperationException(string.Format(Properties.Resources.OperationFailed, kMethodName)); } return(helper.InsertBlockInTargetFile(contextCoordinateSystem.CSEntity, Name, targetFileName)); }
internal override IGeometryEntity[] ProjectOn(DSGeometry other, DSVector direction) { IVector dir = direction.IVector; DSSurface surf = other as DSSurface; if (null != surf) { return(surf.SurfaceEntity.Project(PointEntity, dir)); } DSCurve curve = other as DSCurve; if (null != curve) { IPointEntity pt = curve.CurveEntity.Project(PointEntity, dir); return(new IGeometryEntity[] { pt }); } DSPlane plane = other as DSPlane; if (null != plane) { IPointEntity pt = plane.PlaneEntity.Project(PointEntity, dir); return(new IGeometryEntity[] { pt }); } DSSolid solid = other as DSSolid; if (null != solid) { return(solid.SolidEntity.Project(PointEntity, dir)); } return(base.ProjectOn(other, direction)); }
private DSPoint ProjectOnGeometry(DSGeometry contextGeometry, DSVector direction) { IPointEntity closestPoint = null; if (null == direction) { return(contextGeometry.ClosestPointTo(this)); } else { IGeometryEntity[] entities = ProjectOn(contextGeometry, direction); if (null == entities || entities.Length == 0) { throw new InvalidOperationException(string.Format(Properties.Resources.OperationFailed, "Project along direction")); } int nearestIndex = GetIndexOfNearestGeometry(entities, PointEntity); IGeometryEntity closestGeometry = entities[nearestIndex]; //Clone the closest geometry closestPoint = closestGeometry.Clone() as IPointEntity; //Done with projected entities, dispose them. entities.DisposeObject(); } return(new DSPoint(closestPoint, true)); }
protected override DSGeometry GetGeometryCore(out bool autodispose) { ISurfaceEntity surf = FaceEntity.GetSurfaceGeometry(); autodispose = true; return(DSGeometry.ToGeometry(surf)); }
internal override IGeometryEntity[] ProjectOn(DSGeometry other, DSVector direction) { //Solid solid = other as Solid; //if (null != solid) // return solid.SolidEntity.Project(SurfaceEntity, direction); return(base.ProjectOn(other, direction)); }
protected override void DisposeDisplayable() { if (null != mGeometry && mAutoDispose) { mGeometry.Dispose(); mGeometry = null; } }
/// <summary> /// Translates any geometry type by the given distance in the given /// direction. /// </summary> /// <param name="direction">Displacement direction.</param> /// <param name="distance">Displacement distance along given direction.</param> /// <returns>Transformed Geometry.</returns> public DSGeometry Translate(DSVector direction, double distance) { DSVector offset = direction.Normalize().MultiplyBy(distance); DSGeometry geom = Translate(offset); SetDisplayPropertiesTo(geom.Display); return(geom); }
protected internal DSPlane(DSPoint origin, DSVector normal, double size, bool display = false, DSGeometry context = null) : base(ByOriginNormalCore(origin, ref normal, size), false) { InitializeGuaranteedProperties(); Size = size; Context = context; if (display) { mDisplayPolygon = CreatePlaneVisuals(size, true); } }
/// <summary> /// Highlights the underlying geometry. /// </summary> /// <param name="visibility">flag to highlight/unhighlight</param> /// <returns>true for success</returns> public bool Highlight(bool visibility) { DSGeometry geom = Geometry; if (null != geom) { geom.Highlight(visibility); return(true); } return(false); }
public DSGeometry[] Intersect(DSGeometry other) { if (null == other) { throw new ArgumentNullException("other"); } IGeometryEntity[] geoms = null; DSPlane plane = other as DSPlane; if (plane != null) { geoms = IntersectWithPlane(plane); } else { DSSurface surf = other as DSSurface; if (surf != null) { geoms = IntersectWithSurface(surf); } else { DSSolid solid = other as DSSolid; if (solid != null) { geoms = IntersectWithSolid(solid); } else { DSCurve curve = other as DSCurve; if (curve != null) { geoms = IntersectWithCurve(curve); } else { DSPoint point = other as DSPoint; if (null != point) { geoms = IntersectWithPoint(point); } else { throw new System.InvalidOperationException(string.Format(Properties.Resources.InvalidIntersect, GetType().Name, other.GetType().Name)); } } } } } return(geoms.ToArray <DSGeometry, IGeometryEntity>(true)); }
/// <summary> /// /// </summary> /// <param name="other"></param> /// <param name="direction"></param> /// <returns></returns> public DSGeometry[] Project(DSGeometry other, DSVector direction) { if (null == other) { throw new ArgumentNullException("other"); } IGeometryEntity[] geoms = null; geoms = other.ProjectOn(this, direction); return(geoms.ToArray <DSGeometry, IGeometryEntity>(true)); }
/// <summary> /// /// </summary> /// <param name="offset"></param> /// <returns></returns> protected virtual DSGeometry Translate(DSVector offset) { IGeometryEntity clone = GeomEntity.CopyAndTranslate(offset.IVector); if (null == clone) { throw new InvalidOperationException("Failed to clone and translate geometry."); } DSGeometry geom = ToGeometry(clone, true); return(geom); }
/// <summary> /// Extracts the geometries contained in the block reference /// </summary> /// <returns></returns> public DSGeometry[] ExtractGeometry() { List <DSGeometry> geometries = new List <DSGeometry>(); IGeometryEntity[] entities = BlockEntity.ExtractGeometry(); foreach (var entity in entities) { if (null != entity) { geometries.Add(DSGeometry.ToGeometry(entity, true, this)); } } return(geometries.ToArray()); }
/// <summary> /// Transforms this geometry from source CoordinateSystem to a new /// context CoordinateSystem. /// </summary> /// <param name="fromCoordinateSystem"></param> /// <param name="contextCoordinateSystem"></param> /// <returns>Transformed Geometry.</returns> public DSGeometry Transform(DSCoordinateSystem fromCoordinateSystem, DSCoordinateSystem contextCoordinateSystem) { ICoordinateSystemEntity csEntity = contextCoordinateSystem.CSEntity.PostMultiplyBy(fromCoordinateSystem.CSEntity.Inverse()); DSGeometry geom = TransformBy(csEntity); if (null == geom) { throw new InvalidOperationException(string.Format(Properties.Resources.OperationFailed, "DSGeometry.Transform")); } geom.ContextCoordinateSystem = contextCoordinateSystem; SetDisplayPropertiesTo(geom.Display); return(geom); }
/// <summary> /// /// </summary> /// <returns></returns> public DSGeometry SelectNearest(DSGeometry[] contextGeometries) { if (contextGeometries == null) { throw new System.ArgumentNullException("contextGeometries"); } IGeometryEntity[] hostentities = contextGeometries.ConvertAll(DSGeometryExtension.ToEntity <DSGeometry, IGeometryEntity>); if (hostentities == null) { throw new System.ArgumentException(string.Format(Properties.Resources.InvalidInput, "contextGeometries", "SelectNearest"), "contextGeometries"); } int nearestIndex = GetIndexOfNearestGeometry(hostentities, PointEntity); IGeometryEntity nearestGeom = hostentities[nearestIndex]; return(DSGeometry.ToGeometry(nearestGeom, false)); //returning one of the existing geometry, so persist is no-op. }
/// <summary> /// Extracts the underlying geometry from the topology and makes it /// persistent. /// </summary> /// <param name="color">Color to be assigned to extracted geometry.</param> /// <returns>Geometry</returns> public DSGeometry _ExtractGeometry(DSColor color) { DSGeometry geom = this.Geometry; if (null != geom) { mAutoDispose = false; //someone else is taking control geom.Persist(); if (null != color) { geom.Color = color; } } return(geom); }
public IContextData[] ImportData(Dictionary <string, object> connectionParameters) { List <DSGeometry> geometryList = new List <DSGeometry>(); DSGeometry[] geometry = null; foreach (var param in connectionParameters) { switch (param.Key) { case "OBJ": geometry = DSSubDivisionMesh.ImportFromOBJ(Convert.ToString(param.Value)); break; case "SAT": geometry = DSGeometry.ImportFromSAT(Convert.ToString(param.Value)); break; default: if (param.Value.GetType().IsArray) { Array data = param.Value as Array; geometry = GeometryDataSerializer.CreateGeometryFromData(param.Key, data); } break; } if (null != geometry && geometry.Length > 0) { geometryList.AddRange(geometry); } } int nItems = geometryList.Count; if (nItems == 0) { return(null); } IContextData[] contextData = new IContextData[nItems]; for (int i = 0; i < nItems; ++i) { contextData[i] = new GeometryData("ImportData", geometryList[i], this); } return(contextData); }
/// <summary> /// Creates geometry from given set of data /// </summary> /// <param name="geomType">Geometry type</param> /// <param name="data">Collection of input data to create geometry from</param> /// <returns>Collection of Geometry created using the given data</returns> public static DSGeometry[] CreateGeometryFromData(string geomType, Array data) { Func <GeometryDataSerializer, IDesignScriptEntity> reader = mSerializers.GetReader(geomType); if (reader == null) { return(null); } List <DSGeometry> geometry = new List <DSGeometry>(); GeometryDataSerializer stream = new GeometryDataSerializer(data); while (!stream.DoneReading()) { IGeometryEntity entity = reader(stream) as IGeometryEntity; geometry.Add(DSGeometry.ToGeometry(entity, true)); } return(geometry.ToArray()); }
/// <summary> /// Creates a SubDivisionMesh from Solid or Surface geometry by faceting /// the faces of Solid or Surface. /// </summary> /// <param name="context">Input geometry, Solid or Surface.</param> /// <param name="maxEdgeLength">Maximum allowed edge length /// to define coarseness of the mesh.</param> /// <returns>SubDivisionMesh</returns> public static DSSubDivisionMesh FromGeometry(DSGeometry context, double maxEdgeLength) { string kMethodName = "DSSubDivisionMesh.FromGeometry"; if (null == context) { throw new System.ArgumentNullException("context"); } ISubDMeshEntity entity = HostFactory.Factory.SubDMeshFromGeometry(context.GeomEntity, maxEdgeLength); if (null == entity) { throw new System.InvalidOperationException(string.Format(Properties.Resources.OperationFailed, kMethodName)); } DSSubDivisionMesh mesh = new DSSubDivisionMesh(entity, true); mesh.Context = context; mesh.SubDivisionLevel = 0; return(mesh); }
internal static DSSolid ToSolid(this ISolidEntity host, bool persist, DSGeometry context) { return(host.ToGeometry <DSSolid, ISolidEntity>(persist, context)); }
internal virtual IGeometryEntity[] ProjectOn(DSGeometry other, DSVector direction) { throw new InvalidOperationException(string.Format(Properties.Resources.InvalidProjection, GetType().Name, other.GetType().Name)); }
internal static DSBSplineSurface ToBSurf(this IBSplineSurfaceEntity host, bool persist, DSGeometry context) { return(host.ToGeometry <DSBSplineSurface, IBSplineSurfaceEntity>(persist, context)); }
internal static DSPlane ToPlane(this IPlaneEntity host, bool persist, DSGeometry context) { return(host.ToGeometry <DSPlane, IPlaneEntity>(persist, context)); }