Failed to add AutoText into LO by Ruby

I was trying to add autotext into LO which has a SKP model, I want the autotext refer to comp’s component instance name, but failed, it always appear to be <ComponentInstance>
Code as below:

doc = Layout::Document.open("D://test.layout")
docmodel=doc.pages.first.entities.detect{|e| e.is_a?Layout::SketchUpModel}
skpmodel=Sketchup.active_model
comp=skpmodel.active_entities.detect{|e| e.name=="Sumele"}
point_3d=comp.transformation.origin
point_2d = docmodel.model_to_paper_point(point_3d)
anchor = point_2d
textf = Layout::FormattedText.new("<ComponentInstance>", anchor, Layout::FormattedText::ANCHOR_TYPE_CENTER_CENTER)
text="<ComponentInstance>"
leader_type = Layout::Label::LEADER_LINE_TYPE_SINGLE_SEGMENT
target_point = point_2d
bounds = Geom::Bounds2d.new(2, 1, 1, 1)
label = Layout::Label.new(text, leader_type, target_point, bounds)
doc.add_entity(label, doc.layers.first, doc.pages.first)
cp = Layout::ConnectionPoint.new(docmodel, point_3d,comp.persistent_id.to_s)
label.connect(cp)
label.text=textf

While I refer to it succeed to show page number, I guess there must be some wrong in connection point, but I can’t figure it out.

Please edit your post correcting the code snippet …

THANK YOU

The people figures component are “face me” components whose bounds changes depending upon the camera position. I would say test using a normal component and use a 3D point that actually is touching the instance. Use something simple like a cube.

Using a box component doesn’t work too. even if 3Dpoint is inside the instance.

Can you attach both the .skp and .layout files to the original post please.

And your forum profile is not correct. It says you are using SU2019 but the Free web edition.

testautotext.skp (385.4 KB)
test1.layout (509.8 KB)
File uploaded, it is in Su 2021

Have you tried …

label = Layout::Label.new(
  textf.display_text(doc.pages.first), leader_type, target_point, bounds
)

REF:

Alex I tried it as well, even reordering method calls to no avail.

Perhaps "<ComponentInstance>" is not considered autotext ?

Is the Sketchup.active_model actually the same model as the one in the LayOut document? It might be that the persistend_id of the component doesn’t match any persistent id’s in the SketchUp model viewport’s SketchUp model.

Adam

1 Like

Yes. They are both in the root of my D: drive.
This skpmodel object’s file is the only SKP file in the D root directory.

The docmodel viewport’s reference is pointing at that same model with the same insertion data and time. The “Update” button is gray.

Thanks for looking at this Adam.

Adam I just got a Crash #22320 after trying the code below.

I had edited the text in the <> by putting a space between “Component” and “Instance”, nothing happened. I edited it again and deleted the space.

I right-click the viewport and chose Render Model and Splat!.

Test code ... (click to expand) ...
def label_it
  doc = Layout::Document.open("D:/test1.layout")
  docmodel = doc.pages.first.entities.detect {|e|
    e.is_a?(Layout::SketchUpModel)
  }

  skpmodel = Sketchup.active_model
  comp = skpmodel.active_entities.detect {|e| e.name == "acube" }
  point_3d = comp.transformation.origin

  point_2d = docmodel.model_to_paper_point(point_3d)
  anchor = point_2d

  textf = Layout::FormattedText.new(
    "<ComponentInstance>",
    anchor,
    Layout::FormattedText::ANCHOR_TYPE_CENTER_CENTER
  )
  text = "<ComponentInstance>"
  leader_type = Layout::Label::LEADER_LINE_TYPE_SINGLE_SEGMENT
  target_point = point_2d
  bounds = Geom::Bounds2d.new(2, 1, 1, 1)

  label = Layout::Label.new(text, leader_type, target_point, bounds)
  label.connection_type= Layout::Label::CONNECTION_TYPE_AUTO
  doc.add_entity(label, doc.layers.first, doc.pages.first)

  cp = Layout::ConnectionPoint.new(docmodel, point_3d, comp.persistent_id.to_s)
  label.connect(cp)
  doc.add_entity(textf, doc.layers.first, doc.pages.first)
  label.text = textf

  docmodel.render if docmodel.render_needed?
  doc.save
end

Thanks, Dan, I got more crash in SU2021 :sweat_smile: and Crash with no error message!
The “” is an autotext, if you add an autotext manualy, and edit it to “” works well.

Thanks, Dan, I got more crash in SU2021 :sweat_smile: and Crash with no error message!The<ComponentInstance>is an autotext, if you add an autotext manualy, and edit it to<ComponentInstance>, it works well.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.