Python CAD Tutorial 05 - Drawing lines in 3D space
rss_feed

Python CAD Tutorial 05 - Drawing lines in 3D space

homeHome
pagesGTK-3
pagespython
pagescad
pagesopengl

View/Download Code

Lines

In this tutorial we will create a new line class. Initially this will be setup to create an unlimited number of lines, drawn on every second click of the mouse.Our new class will be similar to the point class, but will contain two points to store the start and stop coordinates of the lines. We will also adjust the draw method to draw lines instead of points.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from point import createpoint


class createline:
    p1 = p2 = None
    color = 0, 1, 0
    display_color = (0, 1, 0)

    def __init__(self, point1, point2):
        """ create the start and stop points and colour of the line"""
        self.p1 = createpoint(point1)
        self.p2 = createpoint(point2)

    def glvertex(self):
        """ Opengl vertex useful so we can dont have to glbegin and glend for each point"""
        glVertex3f(self.p1.x, self.p1.y, self.p1.z)
        glVertex3f(self.p2.x, self.p2.y, self.p2.z)

    def get_points(self):
        """ return the start and end point of the line could be used in a drawing loop"""
        for p in (self.p1, self.p2):
            yield (p)

    def draw(self):
        """ lets draw the line baase on the cordinates, also draw the points for now """
        glColor3f(*self.display_color)

        glBegin(GL_LINES)
        glVertex3f(self.p1.x, self.p1.y, self.p1.z)
        glVertex3f(self.p2.x, self.p2.y, self.p2.z)
        glEnd()

        self.p1.draw()
        self.p2.draw()