Trying to reproduce the behavior of Graph objects with visual representation

Hello everyone.

I am working on a package dealing with a specific type of object that has a visual representation, so I’m trying to reproduce the behavior of the Graph objects in Mathematica. By that, I mean that when an output cell contains a graph it’s visually represented, and you can embed this visual representation into your code for further computing (instead of having the full list of edges).

So far, this is what I have done.

myObject /: MakeBoxes[myObject[data_], StandardForm] := TemplateBox[
  {
   myObject[data],
   ToBoxes@Deploy@visualRepresentation[myObject[data]]
   },
  "myObject",
  DisplayFunction :> (#2 &),
  InterpretationFunction :> (#1 &)]

It’s working properly except that I cannot copy-paste the visual representation and it be evaluated as the object, and I cannot evaluate an output cell. However, I can use % to refer to the output and then it works. Anyone would know how to achieve that?

I was facing the same issue while trying to make the Hypergraph object that behaves exactly like Graph and Tree. I just looked into their FormatValues and copied stuff from there. You can see this hack that overloads GraphicsBox here: Hypergraph/Kernel/Formatting.m at main · WolframInstitute/Hypergraph · GitHub

Excellent, I’ll try to use your code as a template. Thank you!

I posted a simple answer to your question on stack exchange here https://mathematica.stackexchange.com/a/303798/9490

myObject /: MakeBoxes[myObject[data_], form_] := With[
    {interp = Interpretation[visualRepresentation[myObject[data]], myObject[data]]},
    MakeBoxes[interp, form]
]

It works exactly as I wished and is indeed super simple. Thank you so much!