Skip to content

Commit

Permalink
feat(dui3): blocks poc ready(ish) rhino & acad
Browse files Browse the repository at this point in the history
  • Loading branch information
didimitrie committed Jun 17, 2024
1 parent 97d3f65 commit ba4556e
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,12 @@ instanceOrDefinition is InstanceProxy instanceProxy
OpenMode.ForWrite
);

var blockRef = new BlockReference(insertionPoint, definitionId) { BlockTransform = matrix3d };
var blockRef = new BlockReference(insertionPoint, definitionId) { BlockTransform = matrix3d }; // TODO: Bake on correct layer
modelSpaceBlockTableRecord.AppendEntity(blockRef);

if (instanceProxy.applicationId != null)
{
applicationIdMap[instanceProxy.applicationId] = new List<Entity>() { blockRef };
applicationIdMap[instanceProxy.applicationId] = new List<Entity> { blockRef };
}

transaction.AddNewlyCreatedDBObject(blockRef, true);
Expand All @@ -200,6 +200,60 @@ instanceOrDefinition is InstanceProxy instanceProxy
return new(createdObjectIds, consumedObjectIds, conversionResults);
}

public void PurgeInstances(string namePrefix)
{
using var transaction = Application.DocumentManager.CurrentDocument.Database.TransactionManager.StartTransaction();

// Step 1: purge instances and instance definitions
var instanceDefinitionsToDelete = new Dictionary<string, BlockTableRecord>();
using var modelSpaceRecord = Application.DocumentManager.CurrentDocument.Database.GetModelSpace(OpenMode.ForRead);
using var blockTable = (BlockTable)
transaction.GetObject(Application.DocumentManager.CurrentDocument.Database.BlockTableId, OpenMode.ForWrite);

// Recurses through a given block table record and purges inner instances as required.
void TraverseAndClean(BlockTableRecord btr)
{
foreach (var objectId in btr)
{
var obj = transaction.GetObject(objectId, OpenMode.ForRead) as BlockReference;
if (obj == null)
{
continue;
}
var definition = (BlockTableRecord)transaction.GetObject(obj.BlockTableRecord, OpenMode.ForRead);
// POC: this is tightly coupled with a naming convention for definitions in the Instance object manager
if (definition.Name.Contains(namePrefix))
{
obj.UpgradeOpen();
obj.Erase();
TraverseAndClean(definition);
instanceDefinitionsToDelete[obj.BlockTableRecord.ToString()] = definition;
}
}
}

TraverseAndClean(modelSpaceRecord);

// cleanup potentially orphaned definitions (if user deletes an instance reference, we don't reach composing definitions anymore - as such, we need to go through each btr too)
foreach (var btrId in blockTable)
{
var btr = (BlockTableRecord)transaction.GetObject(btrId, OpenMode.ForRead);
if (btr.Name.Contains(namePrefix))
{
TraverseAndClean(btr);
instanceDefinitionsToDelete[btr.Name] = btr;
}
}

foreach (var def in instanceDefinitionsToDelete.Values)
{
def.UpgradeOpen();
def.Erase();
}

transaction.Commit();
}

private Matrix4x4 GetMatrix(double[] t)
{
return new Matrix4x4(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,56 +140,9 @@ CancellationToken cancellationToken

private void PreReceiveDeepClean(string baseLayerPrefix)
{
using var transaction = Application.DocumentManager.CurrentDocument.Database.TransactionManager.StartTransaction();

// Step 1: purge instances and instance definitions
var instanceDefinitionsToDelete = new Dictionary<string, BlockTableRecord>();
using var modelSpaceRecord = Application.DocumentManager.CurrentDocument.Database.GetModelSpace(OpenMode.ForRead);
using var blockTable = (BlockTable)
transaction.GetObject(Application.DocumentManager.CurrentDocument.Database.BlockTableId, OpenMode.ForWrite);

// Recurses through a given block table record and purges inner instances as required.
void TraverseAndClean(BlockTableRecord btr)
{
foreach (var objectId in btr)
{
var obj = transaction.GetObject(objectId, OpenMode.ForRead) as BlockReference;
if (obj == null)
{
continue;
}
var definition = (BlockTableRecord)transaction.GetObject(obj.BlockTableRecord, OpenMode.ForRead);
// POC: this is tightly coupled with a naming convention for definitions in the Instance object manager
if (definition.Name.Contains(baseLayerPrefix))
{
obj.UpgradeOpen();
obj.Erase();
TraverseAndClean(definition);
instanceDefinitionsToDelete[obj.BlockTableRecord.ToString()] = definition;
}
}
}
_instanceObjectsManager.PurgeInstances(baseLayerPrefix);

TraverseAndClean(modelSpaceRecord);

// cleanup potentially orphaned definitions
foreach (var btrId in blockTable)
{
var btr = (BlockTableRecord)transaction.GetObject(btrId, OpenMode.ForRead);
if (btr.Name.Contains(baseLayerPrefix))
{
TraverseAndClean(btr);
instanceDefinitionsToDelete[btr.Name] = btr;
}
}

foreach (var def in instanceDefinitionsToDelete.Values)
{
def.UpgradeOpen();
def.Erase();
}

// Step 2: layers and normal objects
using var transaction = Application.DocumentManager.CurrentDocument.Database.TransactionManager.StartTransaction();
var layerTable = (LayerTable)
transaction.GetObject(Application.DocumentManager.CurrentDocument.Database.LayerTableId, OpenMode.ForRead);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,17 @@ instanceOrDefinition is InstanceProxy instanceProxy
return new(createdObjectIds, consumedObjectIds, conversionResults);
}

public void PurgeInstances(string namePrefix)
{
foreach (var definition in _contextStack.Current.Document.InstanceDefinitions)
{
if (!definition.IsDeleted && definition.Name.Contains(namePrefix))
{
_contextStack.Current.Document.InstanceDefinitions.Delete(definition.Index, true, false);
}
}
}

private Matrix4x4 XFormToMatrix(Transform t) =>
new(t.M00, t.M01, t.M02, t.M03, t.M10, t.M11, t.M12, t.M13, t.M20, t.M21, t.M22, t.M23, t.M30, t.M31, t.M32, t.M33);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,9 @@ private HostObjectBuilderResult BakeObjects(

private void PreReceiveDeepClean(string baseLayerName, int rootLayerIndex)
{
var doc = _contextStack.Current.Document;

// Cleanup blocks/definitions/instances before layers
foreach (var definition in doc.InstanceDefinitions)
{
if (!definition.IsDeleted && definition.Name.Contains(baseLayerName))
{
doc.InstanceDefinitions.Delete(definition.Index, true, false);
}
}
_instanceObjectsManager.PurgeInstances(baseLayerName);

var doc = _contextStack.Current.Document;
// Cleans up any previously received objects
if (rootLayerIndex != RhinoMath.UnsetIntIndex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,32 @@ namespace Speckle.Connectors.Utils.Instances;
public interface IInstanceObjectsManager<THostObjectType, TAppIdMapValueType>
{
/// <summary>
/// Given a list of host application objects, it will unpack them into TODO: comment
/// Given a list of host application objects, it will unpack them into atomic objects, instance proxies and instance proxy definitions.
/// </summary>
/// <param name="objects"></param>
/// <param name="objects">Raw selection from the host application.</param>
UnpackResult<THostObjectType> UnpackSelection(IEnumerable<THostObjectType> objects);

/// <summary>
/// Will bake a set of instance components (instances and instance definitions) in the host app.
/// </summary>
/// <param name="instanceComponents"></param>
/// <param name="applicationIdMap"></param>
/// <param name="baseLayerName"></param>
/// <param name="onOperationProgressed"></param>
/// <returns></returns>
BakeResult BakeInstances(
List<(string[] layerPath, IInstanceComponent obj)> instanceComponents,
Dictionary<string, TAppIdMapValueType> applicationIdMap,
string baseLayerName,
Action<string, double?>? onOperationProgressed
);

/// <summary>
/// <para>Cleans up previously baked instances and associated definitions containing the `namePrefix` in their name.</para>
/// <para>Note: this is based on the convention that all defintions have their name set to a model based prefix.</para>
/// </summary>
/// <param name="namePrefix">The name prefix to search and delete by.</param>
void PurgeInstances(string namePrefix);
}

public record UnpackResult<T>(
Expand Down

0 comments on commit ba4556e

Please sign in to comment.