Skip to content

Commit

Permalink
Update ModelContent conversion.
Browse files Browse the repository at this point in the history
To be consistent with Bake.
  • Loading branch information
kike-garbo committed Aug 27, 2024
1 parent e9464ee commit a52ddf0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 24 deletions.
41 changes: 25 additions & 16 deletions src/RhinoInside.Revit.GH/Types/GeometricElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#if RHINO_8
using Grasshopper.Rhinoceros;
using Grasshopper.Rhinoceros.Model;
using Grasshopper.Rhinoceros.Display;
using Grasshopper.Rhinoceros.Render;
#endif

Expand Down Expand Up @@ -781,6 +782,7 @@ geometryElementContent[0] is ARDB.GeometryInstance geometryInstance &&
{
GeometryDecoder.UpdateGraphicAttributes(g);

var shaded = false;
var geo = default(IGH_GeometricGoo);
switch (g)
{
Expand All @@ -795,6 +797,7 @@ geometryElementContent[0] is ARDB.GeometryInstance geometryInstance &&
var meshGeometry = mesh.ToMesh();
if (!identity) meshGeometry.Transform(transform);
geo = new GH_Mesh(meshGeometry);
shaded = true;
break;

case ARDB.Solid solid:
Expand All @@ -804,6 +807,7 @@ geometryElementContent[0] is ARDB.GeometryInstance geometryInstance &&
if (solidGeometry.TryGetExtrusion(out var extrusion)) geo = new GH_Extrusion(extrusion);
else if (solidGeometry.Faces.Count == 1) geo = new GH_Surface(solidGeometry);
else geo = new GH_Brep(solidGeometry);
shaded = true;
break;

case ARDB.Curve curve:
Expand Down Expand Up @@ -833,24 +837,29 @@ geometryElementContent[0] is ARDB.GeometryInstance geometryInstance &&
var objectAttributes = ModelObject.Cast(geo).ToAttributes();
PeekModelAttributes(idMap, objectAttributes, element.Document);

var context = GeometryDecoder.Context.Peek;
if (context.FaceMaterialId?.Length > 0)
if (shaded)
{
if (context.FaceMaterialId[0].IsValid())
objectAttributes.Display = new ObjectDisplay.Attributes() { Color = ObjectDisplayColor.Value.ByMaterial };

var context = GeometryDecoder.Context.Peek;
if (context.FaceMaterialId?.Length > 0)
{
var faceMaterial = new Material(element.Document, context.FaceMaterialId[0]);
var faceModelMaterial = faceMaterial.ToModelContent(idMap) as ModelRenderMaterial;
objectAttributes.Render = new ObjectRender.Attributes() { Material = faceModelMaterial };

// if (geo is Brep b)
// {
// foreach (var face in b.Faces)
// face.PerFaceColor = faceMaterial.ObjectColor;
// }
// else if (geo is Mesh m)
// {
// m.VertexColors.SetColors(Enumerable.Repeat(faceMaterial.ObjectColor, m.Vertices.Count).ToArray());
// }
if (context.FaceMaterialId[0].IsValid())
{
var faceMaterial = new Material(element.Document, context.FaceMaterialId[0]);
var faceModelMaterial = faceMaterial.ToModelContent(idMap) as ModelRenderMaterial;
objectAttributes.Render = new ObjectRender.Attributes() { Material = faceModelMaterial };

// if (geo is Brep b)
// {
// foreach (var face in b.Faces)
// face.PerFaceColor = faceMaterial.ObjectColor;
// }
// else if (geo is Mesh m)
// {
// m.VertexColors.SetColors(Enumerable.Repeat(faceMaterial.ObjectColor, m.Vertices.Count).ToArray());
// }
}
}
}

Expand Down
20 changes: 20 additions & 0 deletions src/RhinoInside.Revit.GH/Types/Materials/Material.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,26 @@ internal ModelContent ToModelContent(IDictionary<ARDB.ElementId, ModelContent> i
RenderMaterial = material.ToRenderMaterial(Grasshopper.Instances.ActiveRhinoDoc)
};

#if RHINO_8
attributes.RenderMaterial.BeginChange(RenderContent.ChangeContexts.Program);

if (!material.UseRenderAppearanceForShading)
{
var slot = attributes.RenderMaterial.TextureChildSlotName(RenderMaterial.StandardChildSlots.Diffuse);
if (!attributes.RenderMaterial.ChildSlotOn(slot))
{
var diffuse = RenderContentType.NewContentFromTypeId(ContentUuids.SingleColorTextureType) as RenderTexture;
diffuse.Name = material.Name;
diffuse.Fields.Set("color-one", attributes.RenderMaterial.Fields.GetField(RenderMaterial.BasicMaterialParameterNames.Diffuse).GetValue<Rhino.Display.Color4f>());
attributes.RenderMaterial.SetChild(diffuse, slot);
attributes.RenderMaterial.SetChildSlotAmount(slot, 100.0, RenderContent.ChangeContexts.Program);
attributes.RenderMaterial.SetChildSlotOn(slot, true, RenderContent.ChangeContexts.Program);
}
}

attributes.RenderMaterial.Fields.Set(RenderMaterial.BasicMaterialParameterNames.Diffuse, material.Color.ToColor());
#endif

idMap.Add(Id, modelContent = attributes.ToModelData() as ModelContent);
return modelContent;
}
Expand Down
26 changes: 18 additions & 8 deletions src/RhinoInside.Revit.GH/Types/ObjectStyles/Category.cs
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,8 @@ out Guid guid

#region ModelContent
protected override string ElementPath => Parent is Category parent ?
$"{CategoryType}::{parent.ElementPath}::{base.ElementPath}" :
$"{CategoryType}::{base.ElementPath}";
$"{CategoryType}::{parent.Nomen}::{Nomen}" :
$"{CategoryType}::{Nomen}";

#if RHINO_8
internal ModelContent ToModelContent(IDictionary<ARDB.ElementId, ModelContent> idMap)
Expand All @@ -480,7 +480,7 @@ internal ModelContent ToModelContent(IDictionary<ARDB.ElementId, ModelContent> i

// Path
{
attributes.Path = ModelPath;
attributes.Path = $"Revit::{ElementPath}";
};

// Tags
Expand All @@ -493,15 +493,15 @@ internal ModelContent ToModelContent(IDictionary<ARDB.ElementId, ModelContent> i
}
}

// Color
// Linetype
{
var lineColor = category.LineColor.ToColor();
attributes.DisplayColor = lineColor.IsEmpty ? System.Drawing.Color.Black : lineColor;
attributes.Linetype = ProjectionLinePattern?.ToModelContent(idMap) as ModelLinetype ?? ModelLinetype.Unset;
}

// Linetype
// LineColor
{
attributes.Linetype = ProjectionLinePattern?.ToModelContent(idMap) as ModelLinetype ?? ModelLinetype.Unset;
var lineColor = category.LineColor.ToColor();
attributes.DraftingColor = lineColor.IsEmpty ? System.Drawing.Color.Black : lineColor;
}

// LineWeight
Expand All @@ -511,6 +511,16 @@ internal ModelContent ToModelContent(IDictionary<ARDB.ElementId, ModelContent> i
ToLineWeight(ProjectionLineWeight);
}

// Color
{
var displayColor = category.CategoryType != ARDB.CategoryType.Model ||
category.Root().ToBuiltInCategory() == ARDB.BuiltInCategory.OST_Lines ?
(System.Drawing.Color) attributes.DraftingColor :
new Material(category.Material).ObjectColor;

attributes.DisplayColor = displayColor.IsEmpty ? System.Drawing.Color.FromArgb(0x7F, 0x7F, 0x7F) : displayColor;
}

// Material
{
attributes.Material = Material?.ToModelContent(idMap) as ModelRenderMaterial;
Expand Down

0 comments on commit a52ddf0

Please sign in to comment.