Class Block
- Namespace
- CADability.GeoObject
- Assembly
- CADability.dll
A block as a geometric object: a collection of several IGeoObjects which is handled as a single object. The block owns its children (it is their IGeoObjectOwner); cloning the block creates copies of the children. A block has a reference point (RefPoint), which serves as the base point when the block is placed or moved, and may carry a name. Modifying the block modifies all contained objects.
public class Block : IGeoObjectImpl, IGeoObject, ILayer, IStyle, IOctTreeInsertable, IComparable, IFeedBack, ICloneable, IColorDef, ISerializable, IGeoObjectOwner, IDeserializationCallback, IExportStep, IJsonSerialize, IJsonSerializeDone
- Inheritance
-
Block
- Implements
- Derived
- Inherited Members
Constructors
Block()
Creates an empty block. Protected so that blocks are created via Construct(); derived classes call it from their own constructors.
protected Block()
Block(SerializationInfo, StreamingContext)
Constructor required by deserialization
protected Block(SerializationInfo info, StreamingContext context)
Parameters
infoSerializationInfoSerializationInfo
contextStreamingContextStreamingContext
Fields
Constructor
Optional factory for blocks: when set, Construct() calls this delegate instead of creating a plain Block, so that every block CADability creates (when importing a file, grouping objects etc.) is an instance of your derived class. Null by default.
public static Block.ConstructionDelegate Constructor
Field Value
OnPaintTo3D
Hook to replace the painting of blocks: when set, PaintTo3D(IPaintTo3D) calls this delegate first and does nothing more if it returns true. Applies to all blocks. Null by default.
public static Block.PaintTo3DDelegate OnPaintTo3D
Field Value
containedObjects
The objects contained in this block, in drawing order. This is the internal state of the block: derived classes such as Hatch may read and modify it directly, everyone else uses Add(IGeoObject), Remove(int), Item(int) and Children, which keep the owner and the change events of the objects consistent.
protected GeoObjectList containedObjects
Field Value
Properties
Children
Yields a copy of the list of contained geoobjects. Removing objects from that list doesn't remove the objects from the Block.
public GeoObjectList Children { get; }
Property Value
ColorDef
The color of the block. Contained objects whose color is "from parent" (fromParent) are displayed in this color and follow it when it changes; objects with a color of their own are not affected. May be null, the objects "from parent" are then displayed in the default color. Setting it raises the change events and is undoable.
public virtual ColorDef ColorDef { get; set; }
Property Value
Count
Gets the number of GeoObjects in the Block
public int Count { get; }
Property Value
Description
Returns a textual description of the GeoObject. Mainly used for debugging purposes
public override string Description { get; }
Property Value
IsTmpContainer
Marks the block as a temporary container, e.g. the block in which a construction action collects the objects it is about to create in order to display them. In a temporary container the attributes of the block (Layer, ColorDef, Style) are copied to the contained objects whenever they are set, so that the objects carry these attributes when they are taken out of the block later. Setting it to true copies the attributes immediately. False by default; not saved with the block.
public bool IsTmpContainer { get; set; }
Property Value
Layer
Fully implements Layer. Stores the layer in a private member. Setting raises the WillChangeEvent and DidChangeEvent.
public override Layer Layer { get; set; }
Property Value
Name
The name of the block, e.g. the name of the product or assembly when the block stems from a STEP import, or the name of the block definition when it stems from a DXF file. It is written as the product name on STEP export. May be null. Setting it raises the change events and is undoable; it is an attribute change, the geometry is not affected.
public string Name { get; set; }
Property Value
NumChildren
Overrides NumChildren. The default implementation returns 0.
public override int NumChildren { get; }
Property Value
OwnedItems
Returns an array of objects owned by this object. E.g. a face is owned by a shell.
public override IGeoObject[] OwnedItems { get; }
Property Value
Owner
Overrides Owner: when the block is added to or removed from a model, it and its nested blocks subscribe to or unsubscribe from the change event of their color, see CADability.GeoObject.Block.subscribedColorDef.
public override IGeoObjectOwner Owner { get; set; }
Property Value
RefPoint
The reference point of the block in world coordinates: the base point that is used when the block is placed or inserted via a BlockRef. Modify(ModOp) transforms it together with the contained objects. Setting it only moves the reference point, not the contained objects; the change raises the change events and is undoable.
public GeoPoint RefPoint { get; set; }
Property Value
Style
Fully implements Style. Stores the style in a private member. Setting raises the WillChangeEvent and DidChangeEvent.
public override Style Style { get; set; }
Property Value
Methods
Add(GeoObjectList)
Appends all objects of the given list to the block as a single undoable change, otherwise like Add(IGeoObject) for each object. Unlike the single object overload this method does not remove the objects from a previous owner, so pass objects that have no owner yet.
public virtual void Add(GeoObjectList ToAdd)
Parameters
ToAddGeoObjectListThe objects to add
Add(IGeoObject)
Appends an object to the block and makes the block its Owner. If the object currently belongs to another owner (a Model or another block) it is removed from there first. An object whose color is "from parent" is displayed in the color of this block from now on, and changes of the object are forwarded as changes of the block. The change raises the change events and is undoable.
public virtual void Add(IGeoObject ToAdd)
Parameters
ToAddIGeoObjectThe object to add
Child(int)
Overrides Child(int). The default implementation throws a GeoObjectException NoChildren.
public override IGeoObject Child(int Index)
Parameters
Indexint
Returns
Clear()
Removes all objects from that block and returns those objects in a new list. The returned objects have no Owner.
public GeoObjectList Clear()
Returns
- GeoObjectList
a list with all the objects of this block
Clone()
Overrides Clone(). Returns a new Block, that contains clones of the children of this Block. An IGeoObject cannot be contained by two different block objects, because there may be only one owner (see also Owner).
public override IGeoObject Clone()
Returns
Construct()
Creates a new, empty block. This is the only public way to create a block, the constructor is protected. If a Constructor is registered, the block is created by that delegate, otherwise a plain Block is returned. Fill it with Add(IGeoObject), Add(GeoObjectList) or Set(GeoObjectList).
public static Block Construct()
Returns
- Block
The new block, which contains no objects yet
CopyGeometry(IGeoObject)
Copies the geometrical aspects of the given GeoObject to this GeoObject. Both objects must be of the same type. The attributes are not copied.
public override void CopyGeometry(IGeoObject ToCopyFrom)
Parameters
ToCopyFromIGeoObjectGeoObject to copy from
Decompose()
Decomposes the object into simpler objects, see Decompose(). The default implementation returns null: the object cannot be decomposed.
public override GeoObjectList Decompose()
Returns
- GeoObjectList
the parts, or null
FindSnapPoint(SnapPointFinder)
Asks the object to add its snap points (end points, centres, intersections...) of the kinds that
spf asks for. The default implementation adds none; objects with geometry override it.
public override void FindSnapPoint(SnapPointFinder spf)
Parameters
spfSnapPointFinderdefinition and collection of the snap points
GetBoundingCube()
Returns a bounding cube of the object. The object must fir into this cube. There may be a smaller cube that contains the object if it is to expensive to calculate the exact cube.
public override BoundingCube GetBoundingCube()
Returns
- BoundingCube
the bounding cube
GetExtent(Projection, ExtentPrecision)
Returns the 2d extent of the object as it appears in the given projection, in the coordinates of the projection's plane (not in pixels).
public override BoundingRect GetExtent(Projection projection, ExtentPrecision extentPrecision)
Parameters
projectionProjectionthe projection
extentPrecisionExtentPrecisionExact for the exact extent, Raw for a quick estimate that may be larger
Returns
- BoundingRect
the extent in the projection's 2d coordinates
GetExtent(double)
Gets the 3-dimensional extent of an object
public override BoundingCube GetExtent(double precision)
Parameters
precisiondoublePrecision of the test
Returns
- BoundingCube
The minmax cube
GetObjectData(IJsonWriteData)
Need to make it public in order to allow inheriting classes to acces this method via base.GetObjectData
public override void GetObjectData(IJsonWriteData data)
Parameters
dataIJsonWriteData
GetObjectData(SerializationInfo, StreamingContext)
Writes the data of this object for the legacy binary serialization (ISerializable). Files are written in the JSON format (IJsonSerialize) now; the binary format is still read.
public override void GetObjectData(SerializationInfo info, StreamingContext context)
Parameters
infoSerializationInforeceives the data
contextStreamingContextthe destination of the serialization
GetQuadTreeItem(Projection, ExtentPrecision)
Returns an item for a 2d quad tree of the projected objects, used for hit tests in a projection: it knows the 2d extent of the object, whether a 2d rectangle hits it, and the depth (z position) of the object at a 2d point. Composite objects (blocks, solids, paths) return a collection of the items of their parts.
public override IQuadTreeInsertableZ GetQuadTreeItem(Projection projection, ExtentPrecision extentPrecision)
Parameters
projectionProjectionthe projection
extentPrecisionExtentPrecisionprecision of the extent, see GetExtent(Projection, ExtentPrecision)
Returns
- IQuadTreeInsertableZ
the item for the quad tree
GetShowProperties(IFrame)
Should be overridden and return a IPropertyEntry derived object that handles the display and modification of the properties of the IGeoObject derived object. Default implementation return null.
public override IPropertyEntry GetShowProperties(IFrame Frame)
Parameters
FrameIFrame
Returns
HasChildren()
Overrides HasChildren(). The deafult implementation returns false.
public override bool HasChildren()
Returns
HitTest(ref BoundingCube, double)
Tests whether an object is touched by the provided cube
public override bool HitTest(ref BoundingCube cube, double precision)
Parameters
cubeBoundingCubeTo test with
precisiondoubleRequired precision
Returns
- bool
true if the object strikes the cube
HitTest(Projection, BoundingRect, bool)
Tests whether an object is inside or touched by the rectangle when projected with the provided projection
public override bool HitTest(Projection projection, BoundingRect rect, bool onlyInside)
Parameters
projectionProjectionThe projection for the test
rectBoundingRectThe rectangle to test the position
onlyInsidebooltrue: the object must be totaly inside the rectangle, false: the object must be totaly or partially inside the rectangle
Returns
- bool
true when the test succeedes, false otherwise
HitTest(PickArea, bool)
Tests whether an object is inside or touched by the provided area
public override bool HitTest(Projection.PickArea area, bool onlyInside)
Parameters
areaProjection.PickAreaThe area to check for
onlyInsidebooltrue: the object must be totaly inside the area, false: the object must be totaly or partially inside the area
Returns
- bool
true when the test succeedes, false otherwise
Insert(int, IGeoObject)
Inserts an object at the given position in the drawing order of the block. Apart from the position this behaves like Add(IGeoObject): the object is removed from a previous owner and becomes owned by this block, the change raises the change events and is undoable.
public virtual void Insert(int index, IGeoObject ToAdd)
Parameters
indexintPosition at which the object is inserted, 0 <= index <= Count
ToAddIGeoObjectThe object to insert
Item(int)
Returns the contained object at the given position in the drawing order.
public IGeoObject Item(int Index)
Parameters
Returns
- IGeoObject
The object itself (not a copy); it still belongs to the block
Modify(ModOp)
Modifies this GeoObject with the given modification operation (includes moving, rotating, reflecting, scaling etc.).
public override void Modify(ModOp m)
Parameters
mModOpOperation to be applied
OnDeserialization(object)
Implements OnDeserialization(object) for the legacy binary format: after the whole object graph has been read, the block registers itself as owner of its contained objects, subscribes to their change events and reconnects the colors "from parent". Called by the deserialization, not meant to be called by user code.
public virtual void OnDeserialization(object sender)
Parameters
senderobjectNot used
PaintTo3D(IPaintTo3D)
Paints the object with the methods of paintTo3D: lines, triangles, text... The views implement
IPaintTo3D (with OpenGL or GDI) and collect what the object paints in a display list, so this is
called rarely, after PrepareDisplayList(double) has done the expensive work (triangulation, approximation).
public override void PaintTo3D(IPaintTo3D paintTo3D)
Parameters
paintTo3DIPaintTo3Dwhere to paint
PaintTo3DList(IPaintTo3D, ICategorizedDislayLists)
Adds this object as a whole to the display lists, in the category of its layer. Suitable for simple objects like curves and text; composite objects (solids, shells, blocks) override it and add their parts by category.
public override void PaintTo3DList(IPaintTo3D paintTo3D, ICategorizedDislayLists lists)
Parameters
paintTo3DIPaintTo3Dthe paint interface of the view
listsICategorizedDislayListsthe display lists to add to
Position(GeoPoint, GeoVector, double)
Returns the smallest parameter value (l) where the provided line hits the object so that the point
fromHere + l * direction is on or close to the object. The result may be negative.
Return double.MaxValue if there is no such point.
public override double Position(GeoPoint fromHere, GeoVector direction, double precision)
Parameters
fromHereGeoPointStartpoint of the line
directionGeoVectorDirection of the line
precisiondoublePrecision for the test
Returns
- double
Position on the line
PrePaintTo3D(IPaintTo3D)
Called before PaintTo3D(IPaintTo3D), outside of the display list that collects what PaintTo3D paints. Override it for work that must not go into that list, e.g. painting that needs a display list of its own. The default implementation does nothing.
public override void PrePaintTo3D(IPaintTo3D paintTo3D)
Parameters
paintTo3DIPaintTo3Dthe paint interface of the view
PrepareDisplayList(double)
Called before PaintTo3D(IPaintTo3D) is called, should implement the time consuming work for the display (like calculationg the triangulation of Faces or Polylines of Curves). Not normally used by user code.
public override void PrepareDisplayList(double precision)
Parameters
precisiondoubleRequired precision
Remove(GeoObjectList)
Removes all objects of the given list from the block, see Remove(int). Objects that are not contained in this block are ignored. The change raises the change events and is undoable as a single step.
public virtual void Remove(GeoObjectList ToRemove)
Parameters
ToRemoveGeoObjectListThe objects to remove
Remove(int)
Removes the object at the given position from the block. The object loses its owner and is no longer displayed with the block; an object whose color is "from parent" no longer follows the color of this block. The change raises the change events and is undoable.
public virtual void Remove(int Index)
Parameters
ReplaceContent(GeoObjectList)
Replace the list of child objects without fireing a chang event. This is used by derived classes (e.g. Hatch or Dimension or user derived classes to recalculate their contents. The derived classes already have fired the change event.
protected void ReplaceContent(GeoObjectList NewContent)
Parameters
NewContentGeoObjectListList of child objects to replace the existing list
SerializationDone(JsonSerialize)
Implements SerializationDone(JsonSerialize). After reading from JSON the contained objects are completed first, then the block registers itself as their owner, subscribes to their change events and reconnects the colors "from parent". When writing there is nothing to do. Called by the serialization, not meant to be called by user code.
public virtual void SerializationDone(JsonSerialize jsonSerialize)
Parameters
jsonSerializeJsonSerializeThe serializer; IsWriting tells the direction
Set(GeoObjectList)
Replaces the contents of the block by the objects of the given list. The objects contained so far lose their owner, the new objects become owned by this block. The objects are not cloned, so pass copies if the originals are still in use elsewhere. The change raises the change events and is undoable.
public virtual void Set(GeoObjectList toSet)
Parameters
toSetGeoObjectListThe new contents of the block
SetObjectData(IJsonReadData)
Reads the data written by GetObjectData(IJsonWriteData) back from the JSON serialization. Derived classes override it, call the base implementation and read their own properties. Objects referenced by the data may still be incomplete at this time, see IJsonSerializeDone.
public override void SetObjectData(IJsonReadData data)
Parameters
dataIJsonReadDataprovides the data