Project_to_line problem

Hello everyone.

First. I want to check line passing through the point(5,5,5).

line1 = [Geom::Point3d.new(0,0,0), Geom::Point3d.new(10,0,10)]
line2 = [Geom::Point3d.new(10,0,0), Geom::Point3d.new(0,10,0)]
line3 = [Geom::Point3d.new(0,10,0), Geom::Point3d.new(10,10,0)]
line4 = [Geom::Point3d.new(10,10,0), Geom::Point3d.new(0,0,10)]
point = [5,5,5]

projected_point =
projected_point[0] = point.project_to_line line1
projected_point[1] = point.project_to_line line2
projected_point[2] = point.project_to_line line3
projected_point[3] = point.project_to_line line4

is this well work. and i’m try to check selected line passing through the point.

but i don’t have idea how can check the project point to selected line

model = Sketchup.active_model
sel = Sketchup.active_model.selection
ent = model.active_entities
ent = entities[0]
status = sel.add ent
point = Geom::Point3d.new 0,0,0
projected_point = point.project_to_line ent

this code return wrong argument type.

So. Will I need to modify any part of the code?

Yes.

The API defines the constant ORIGIN as a Geom::Point3d object with coordinates: [0,0,0].

should be:

ents = model.active_entities
ent = ents[0]

… BUT if the user is making the selection, then just:

ents = model.active_entities
ent = sel[0]

However, you are mixing virtual geometic helper objects and actual model drawing element objects.

A line is a geometric helper which is virtual. It passes through two points but is infinite in length. It has no ends. The model entities collection does not have lines. Lines are only for virtual geometric work in memory.

The SketchUp entities collection has edge objects. They have finite length. Each has a start vertex, and an end vertex.

Sketchup::Vertex objects are in the model. But they are not Geom::Point3d objects. You can get a Geom::Point3d equivalent from a vertex object, via:

vertex = edge.start
vertex.class
>> Sketchup::Vertex
start_point = vertex.position()
start_point.class
>> Geom::Point3d

Ok so, say your user has selected an edge object:

ents = model.active_entities
ent = model.selection[0]
if ent.is_a?(Sketchup::Edge)
  ORIGIN.on_line?( [ent.start.position, ent.end.position] )
end

http://www.sketchup.com/intl/en/developer/docs/ourdoc/point3d#on_line?

If that test is true, the test point is on the line of the edge, but may be outside the vertices of the edge. So test if the sum of the distances from the test point to the edge’s end points are equal to the edge’s length. If true the test point is somewhere along the edge. If false, the test point is outside the edge.

ent.length == ORIGIN.distance(ent.start.position)+ORIGIN.distance(ent.end.position)

http://www.sketchup.com/intl/en/developer/docs/ourdoc/point3d#distance


ALSO > How to properly quote code in the forums

Thanks DanRathbun you save me!!