# Project\_to\_line problem

**URL:** <https://forums.sketchup.com/t/project-to-line-problem/27455>\
**Category:** Ruby API\
**Created:** [July 5, 2016, 2:41am UTC](https://forums.sketchup.com/t/project-to-line-problem/27455 "2016-07-05T02:41:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![perfumeindream](https://avatars.discourse-cdn.com/v4/letter/p/f04885/32.png) [@perfumeindream](https://forums.sketchup.com/u/perfumeindream)\
**Post date:** [July 5, 2016, 2:41am UTC](https://forums.sketchup.com/t/project-to-line-problem/27455/1 "2016-07-05T02:41:27Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [July 5, 2016, 4:24am UTC](https://forums.sketchup.com/t/project-to-line-problem/27455/2 "2016-07-05T04:24:03Z")

</div>

> [@perfumeindream](#):
>
> So. Will I need to modify any part of the code?

Yes.

> [@perfumeindream](#):
>
> `point = Geom::Point3d.new 0,0,0`

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

> [@perfumeindream](#):
>
> ```ruby
> ent = model.active_entities
> ent = entities[0]
> 
> ```

should be:

```ruby
ents = model.active_entities
ent = ents[0]

```

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

```ruby
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:

```ruby
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:

```ruby
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](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.

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

```

[http://www.sketchup.com/intl/en/developer/docs/ourdoc/point3d#distance](http://www.sketchup.com/intl/en/developer/docs/ourdoc/point3d#distance)

* * *

**ALSO \>** [How to properly quote code in the forums](http://forums.sketchup.com/t/how-to-properly-quote-code-in-the-forums/13614/2)

---

<div class="post-metadata">

**Author:** ![perfumeindream](https://avatars.discourse-cdn.com/v4/letter/p/f04885/32.png) [@perfumeindream](https://forums.sketchup.com/u/perfumeindream)\
**Post date:** [July 5, 2016, 4:54am UTC](https://forums.sketchup.com/t/project-to-line-problem/27455/3 "2016-07-05T04:54:02Z")

</div>

Thanks DanRathbun you save me!!
