Hello everyone!
I have a problem. I implemented a base component.
but i move the obeject in Rhino doc,preiview does not redraw ,like this.
Maybe I forgot to implement something…
So anyone can help me?
this is my code.
public class TextEntityGoo : GH_GeometricGoo<TextEntity>, IGH_PreviewData, IGH_BakeAwareData
{
public override BoundingBox Boundingbox => Value?.GetBoundingBox(false) ?? BoundingBox.Empty;
public BoundingBox ClippingBox => Boundingbox;
public override bool IsValid => Value != null;
public override string TypeDescription => "TextEntity";
public override string TypeName => "TextEntity";
public TextEntityGoo()
{
Value = new TextEntity();
}
public TextEntityGoo(TextEntityGoo textEntityGoo)
{
Value = textEntityGoo.Value.Duplicate() as TextEntity;
}
public TextEntityGoo(TextEntity textEntity) : base(textEntity)
{
}
public bool BakeGeometry(RhinoDoc doc, ObjectAttributes att, out Guid obj_guid)
{
if (Value == null)
{
obj_guid = Guid.Empty;
return false;
}
obj_guid = doc.Objects.AddText(Value);
return true;
}
public override bool CastFrom(object source)
{
if (source is TextEntity entity)
{
Value = entity;
return true;
}
return false;
}
public override bool CastTo<Q>(out Q target)
{
if (typeof(Q).IsAssignableFrom(typeof(TextEntity)))
{
target = (Q)(object)Value;
return true;
}
target = default;
return false;
}
public void DrawViewportMeshes(GH_PreviewMeshArgs args)
{
}
public void DrawViewportWires(GH_PreviewWireArgs args)
{
if (Value == null) return;
args.Pipeline.DrawText(Value, args.Color);
}
public override IGH_Goo Duplicate()
{
return new TextEntityGoo(this);
}
public override IGH_GeometricGoo DuplicateGeometry()
{
var entity = Value.Duplicate() as TextEntity;
return new TextEntityGoo(entity);
}
public override BoundingBox GetBoundingBox(Transform xform)
{
var entity = Value.Duplicate() as TextEntity;
entity.Transform(xform);
return entity.GetBoundingBox(false);
}
public override IGH_GeometricGoo Morph(SpaceMorph xmorph)
{
var entity = Value.Duplicate() as TextEntity;
xmorph.Morph(entity);
return new TextEntityGoo(entity);
}
public override bool Read(GH_IReader reader)
{
//var text = string.Empty;
//GH_IO.Types.GH_Plane pln = new GH_IO.Types.GH_Plane();
//var flag = reader.TryGetString("text", ref text);
//reader.TryGetPlane("plane", ref pln);
return base.Read(reader);
}
public override object ScriptVariable()
{
return Value.PlainText;
}
public override string ToString()
{
if (m_value != null)
{
return m_value.PlainText;
}
else
{
return "<null>";
}
}
public override IGH_GeometricGoo Transform(Transform xform)
{
var entity = Value.Duplicate() as TextEntity;
entity.Transform(xform);
return new TextEntityGoo(entity);
}
public override bool Write(GH_IWriter writer)
{
return base.Write(writer);
}
}
public class Param_TextEntity : GH_PersistentParam<TextEntityGoo>, IGH_BakeAwareObject, IGH_PreviewObject
{
public BoundingBox ClippingBox => Preview_ComputeClippingBox();
public override Guid ComponentGuid => Guid.Parse("117CAC76-F72E-43D7-B7F9-91562855A93C");
public override GH_Exposure Exposure => GH_Exposure.secondary;
public bool Hidden { get; set; }
public bool IsBakeCapable => true;
public bool IsPreviewCapable => true;
public override string TypeName => nameof(TextEntity);
protected override Bitmap Icon => Properties.Resources.文本;
public Param_TextEntity() : base("TextEntity", null, "文本", "Params", "Geometry")
{
}
public void BakeGeometry(RhinoDoc doc, List<Guid> obj_ids)
{
BakeGeometry(doc, null, obj_ids);
}
public void BakeGeometry(RhinoDoc doc, ObjectAttributes att, List<Guid> obj_ids)
{
var util = new GH_BakeUtility(OnPingDocument());
util.BakeObjects(m_data, att, doc);
obj_ids.AddRange(util.BakedIds);
}
public void DrawViewportMeshes(IGH_PreviewArgs args)
{
}
public void DrawViewportWires(IGH_PreviewArgs args)
{
Preview_DrawWires(args);
}
public override void RemoveEffects()
{
RemoveEffects();
}
protected override TextEntityGoo InstantiateT()
{
return new TextEntityGoo();
}
protected override TextEntityGoo PreferredCast(object data)
{
return data is TextEntityGoo goo ? goo : null;
}
protected override GH_GetterResult Prompt_Plural(ref List<TextEntityGoo> values)
{
var get = new GetObject();
get.SetCommandPrompt("选择多个文本");
get.SetCustomGeometryFilter(ObjectFilterDelegation.FilterTextEntity);
get.GetMultiple(1, 0);
if (get.CommandResult() != Rhino.Commands.Result.Success)
{
return GH_GetterResult.cancel;
}
values = get.Objects().Select(o => new TextEntityGoo(o.TextEntity())).ToList();
return GH_GetterResult.success;
}
protected override GH_GetterResult Prompt_Singular(ref TextEntityGoo value)
{
var get = new GetObject();
get.SetCommandPrompt("选择一个文本");
get.SetCustomGeometryFilter(ObjectFilterDelegation.FilterTextEntity);
get.Get();
if (get.CommandResult() != Rhino.Commands.Result.Success)
{
return GH_GetterResult.cancel;
}
var entity = get.Object(0).TextEntity();
value = new TextEntityGoo(entity);
return GH_GetterResult.success;
}
}