Line and distance between selected cpoints


#1

Hello,

I’m looking for a part of code that draws a line between selected cpoints and get the distance.

To get the selected cpoints you can use:

m=Sketchup.active_model; s=m.selection; s.grep(Sketchup::ConstructionPoint);

but how can you transfer the collected points to usable points to create lines or get the distance between it?

Thanks


#2

Let’s assume:

cpts=s.grep(Sketchup::ConstructionPoint)

Then:

cpts.each{|cpt|
  ### do something with each cpt here
}

E.G. to find the position of each ‘cpoint’ use:

pt=cpt.position

This is a ‘point3d’ object.

If you want to draw a line to another point, use:

edg=m.active_entities.add_line(pt, some_other_point)

To find the distance from the pt to some_other_point, use:

dis=pt.distance(some_other_point)


#3

Thanks again Tig,

I only get the following error:

undefined local variable or method ‘some_other_point’ for #<AS_RubyEditor::RubyEditor:0x000000078d3cc0

when i do this:

m=Sketchup.active_model; s=m.selection; cpts=s.grep(Sketchup::ConstructionPoint)

cpts.each{|cpt|
 ### do something with each cpt here
 pt=cpt.position 
 edg=m.active_entities.add_line(pt, some_other_point)
}


#4

Where and when did you define some_other_point? It either doesn’t exist at all, or is in a namespace that isn’t visible inside the each block.


#5

The clue is in the error message !
If you don’t define ‘some_other_point’ as a point3d or a three element array, then it is going to fail !!

Please read what you type, and what error messages you get back.
I only used ‘some_other_point’ as a ‘placeholder’ in the code example - you need to expend at least a little effort to get it to work like you want it to…


#6

A thanks sorry and i see the glue now :slight_smile: i was i little bit to fast