Skip to content

Commit 4d36035

Browse files
committed
Merge pull request #119 from tpaviot/review/new-examples
Added more examples
2 parents fafc6f4 + 9157d7a commit 4d36035

18 files changed

+1016
-1
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env python
2+
3+
##Copyright 2009-2015 Thomas Paviot ([email protected])
4+
##
5+
##This file is part of pythonOCC.
6+
##
7+
##pythonOCC is free software: you can redistribute it and/or modify
8+
##it under the terms of the GNU Lesser General Public License as published by
9+
##the Free Software Foundation, either version 3 of the License, or
10+
##(at your option) any later version.
11+
##
12+
##pythonOCC is distributed in the hope that it will be useful,
13+
##but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
##GNU Lesser General Public License for more details.
16+
##
17+
##You should have received a copy of the GNU Lesser General Public License
18+
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
19+
20+
21+
from OCC.AIS import AIS_Shape
22+
from OCC.BRepPrimAPI import BRepPrimAPI_MakeBox
23+
from OCC.Display.SimpleGui import init_display
24+
display, start_display, add_menu, add_function_to_menu = init_display()
25+
26+
#
27+
# Create a box
28+
#
29+
s = BRepPrimAPI_MakeBox(200, 100, 50).Shape()
30+
#
31+
# Create an AIS_Shape from the previous shape
32+
#
33+
ais_shp = AIS_Shape(s)
34+
ais_shp.SetWidth(4)
35+
ais_shp.SetTransparency(0.10)
36+
37+
#
38+
# Get context and display shape
39+
#
40+
# Get Context
41+
ais_context = display.GetContext().GetObject()
42+
ais_context.Display(ais_shp.GetHandle())
43+
44+
display.View_Iso()
45+
display.FitAll()
46+
start_display()
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env python
2+
3+
##Copyright 2009-2015 Thomas Paviot ([email protected])
4+
##
5+
##This file is part of pythonOCC.
6+
##
7+
##pythonOCC is free software: you can redistribute it and/or modify
8+
##it under the terms of the GNU Lesser General Public License as published by
9+
##the Free Software Foundation, either version 3 of the License, or
10+
##(at your option) any later version.
11+
##
12+
##pythonOCC is distributed in the hope that it will be useful,
13+
##but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
##GNU Lesser General Public License for more details.
16+
##
17+
##You should have received a copy of the GNU Lesser General Public License
18+
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
19+
20+
#
21+
# This sample shows howto set display quality (higher or lower).
22+
# Be carful that improving quality results in higher memory consumption
23+
#
24+
25+
from OCC.BRepPrimAPI import BRepPrimAPI_MakeCylinder
26+
from OCC.Display.SimpleGui import init_display
27+
display, start_display, add_menu, add_function_to_menu = init_display()
28+
display.SetModeHLR()
29+
#
30+
# Get Context
31+
#
32+
ais_context = display.GetContext().GetObject()
33+
#
34+
# Get Prs3d_drawer from previous context
35+
#
36+
drawer_handle = ais_context.DefaultDrawer()
37+
drawer = drawer_handle.GetObject()
38+
drawer.SetIsoOnPlane(True)
39+
40+
la = drawer.LineAspect().GetObject()
41+
la.SetWidth(4)
42+
# increase line width in the current viewer
43+
# This is only viewed in the HLR mode (hit 'e' key for instance)
44+
line_aspect = drawer.SeenLineAspect().GetObject()
45+
drawer.EnableDrawHiddenLine()
46+
line_aspect.SetWidth(4)
47+
#
48+
drawer.SetWireAspect(line_aspect.GetHandle())
49+
#
50+
# Displays a cylinder
51+
#
52+
s = BRepPrimAPI_MakeCylinder(50., 50.).Shape()
53+
display.DisplayShape(s)
54+
#
55+
# Display settings and display loop
56+
#
57+
display.View_Iso()
58+
display.FitAll()
59+
start_display()
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
##Copyright 2010-2015 Thomas Paviot ([email protected])
2+
##
3+
##This file is part of pythonOCC.
4+
##
5+
##pythonOCC is free software: you can redistribute it and/or modify
6+
##it under the terms of the GNU Lesser General Public License as published by
7+
##the Free Software Foundation, either version 3 of the License, or
8+
##(at your option) any later version.
9+
##
10+
##pythonOCC is distributed in the hope that it will be useful,
11+
##but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
##GNU Lesser General Public License for more details.
14+
##
15+
##You should have received a copy of the GNU Lesser General Public License
16+
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
17+
18+
from OCC.Display.SimpleGui import init_display
19+
from OCC.BRepPrimAPI import BRepPrimAPI_MakeBox
20+
21+
display, start_display, add_menu, add_function_to_menu = init_display()
22+
my_box = BRepPrimAPI_MakeBox(10., 20., 30.).Shape()
23+
24+
display.DisplayShape(my_box, update=True)
25+
26+
# in order to be able to export to usual file formats
27+
# oce must have been compiled with FreeImage
28+
# otherwise, images will be exported to PPM format
29+
30+
def export_to_BMP(event=None):
31+
display.View.Dump('./capture_bmp.bmp')
32+
33+
34+
def export_to_PNG(event=None):
35+
display.View.Dump('./capture_png.png')
36+
37+
38+
def export_to_JPEG(event=None):
39+
display.View.Dump('./capture_jpeg.jpeg')
40+
41+
42+
def export_to_TIFF(event=None):
43+
display.View.Dump('./capture_tiff.tiff')
44+
45+
46+
def exit(event=None):
47+
sys.exit()
48+
49+
if __name__ == '__main__':
50+
add_menu('screencapture')
51+
add_function_to_menu('screencapture', export_to_BMP)
52+
add_function_to_menu('screencapture', export_to_PNG)
53+
add_function_to_menu('screencapture', export_to_JPEG)
54+
add_function_to_menu('screencapture', export_to_TIFF)
55+
add_function_to_menu('screencapture', exit)
56+
start_display()

examples/core_display_quality.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env python
2+
3+
##Copyright 2009-2015 Thomas Paviot ([email protected])
4+
##
5+
##This file is part of pythonOCC.
6+
##
7+
##pythonOCC is free software: you can redistribute it and/or modify
8+
##it under the terms of the GNU Lesser General Public License as published by
9+
##the Free Software Foundation, either version 3 of the License, or
10+
##(at your option) any later version.
11+
##
12+
##pythonOCC is distributed in the hope that it will be useful,
13+
##but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
##GNU Lesser General Public License for more details.
16+
##
17+
##You should have received a copy of the GNU Lesser General Public License
18+
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
19+
20+
#
21+
# This sample shows howto set display quality (higher or lower).
22+
# Be carful that improving quality results in higher memory consumption
23+
#
24+
25+
from OCC.BRepPrimAPI import BRepPrimAPI_MakeCylinder
26+
from OCC.Display.SimpleGui import init_display
27+
display, start_display, add_menu, add_function_to_menu = init_display()
28+
29+
30+
#
31+
# Get Context
32+
#
33+
ais_context = display.GetContext().GetObject()
34+
#
35+
# Display current quality
36+
dc = ais_context.DeviationCoefficient()
37+
dc_hlr = ais_context.HLRDeviationCoefficient()
38+
da = ais_context.DeviationAngle()
39+
da_hlr = ais_context.HLRAngle()
40+
print("Default display quality settings:")
41+
print("Deviation Coefficient: %f" % dc)
42+
print("Deviation Coefficient Hidden Line Removal: %f" % dc_hlr)
43+
print("Deviation Angle: %f" % da)
44+
print("Deviation Angle Hidden Line Removal: %f" % da_hlr)
45+
#
46+
# Improve quality by a factor 10
47+
#
48+
factor = 20
49+
ais_context.SetDeviationCoefficient(dc/factor)
50+
ais_context.SetDeviationAngle(da/factor)
51+
ais_context.SetHLRDeviationCoefficient(dc_hlr/factor)
52+
ais_context.SetHLRAngle(da_hlr/factor)
53+
print("Quality display improved by a factor {0}".format(factor))
54+
#
55+
# Displays a cylinder
56+
#
57+
s = BRepPrimAPI_MakeCylinder(50., 50.).Shape()
58+
display.DisplayShape(s)
59+
#
60+
# Display settings and display loop
61+
#
62+
display.View_Iso()
63+
display.FitAll()
64+
start_display()
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env python
2+
3+
##Copyright 2009-2015 Thomas Paviot ([email protected])
4+
##
5+
##This file is part of pythonOCC.
6+
##
7+
##pythonOCC is free software: you can redistribute it and/or modify
8+
##it under the terms of the GNU Lesser General Public License as published by
9+
##the Free Software Foundation, either version 3 of the License, or
10+
##(at your option) any later version.
11+
##
12+
##pythonOCC is distributed in the hope that it will be useful,
13+
##but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
##GNU Lesser General Public License for more details.
16+
##
17+
##You should have received a copy of the GNU Lesser General Public License
18+
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
19+
20+
import os
21+
22+
from OCC.Graphic3d import Graphic3d_NOM_SILVER, Graphic3d_MaterialAspect
23+
from OCC.BRepPrimAPI import BRepPrimAPI_MakeCylinder
24+
25+
from OCC.Display.SimpleGui import init_display
26+
display, start_display, add_menu, add_function_to_menu = init_display()
27+
28+
29+
class Texture(object):
30+
"""
31+
This class encapsulates the necessary texture properties:
32+
Filename, toScaleU, etc.
33+
"""
34+
def __init__(self, filename):
35+
if not os.path.isfile(filename):
36+
raise IOError("File %s not found.\n" % filename)
37+
self._filename = filename
38+
self._toScaleU = 1.0
39+
self._toScaleV = 1.0
40+
self._toRepeatU = 1.0
41+
self._toRepeatV = 1.0
42+
self._originU = 0.0
43+
self._originV = 0.0
44+
45+
def TextureScale(self, toScaleU, toScaleV):
46+
self._toScaleU = toScaleU
47+
self._toScaleV = toScaleV
48+
49+
def TextureRepeat(self, toRepeatU, toRepeatV):
50+
self._toRepeatU = toRepeatU
51+
self._toRepeatV = toRepeatV
52+
53+
def TextureOrigin(self, originU, originV):
54+
self._originU = originU
55+
self._originV = originV
56+
57+
def GetProperties(self):
58+
return (self._filename,
59+
self._toScaleU, self._toScaleV,
60+
self._toRepeatU, self._toRepeatV,
61+
self._originU, self._originV)
62+
63+
#
64+
# First create texture and a material
65+
#
66+
texture_filename = './images/ground.bmp'
67+
t = Texture(texture_filename)
68+
m = Graphic3d_MaterialAspect(Graphic3d_NOM_SILVER)
69+
#
70+
# Displays a cylinder with a material and a texture
71+
#
72+
s = BRepPrimAPI_MakeCylinder(60, 200)
73+
display.DisplayShape(s.Shape(), material=m, texture=t)
74+
#
75+
# Display settings
76+
#
77+
display.View_Iso()
78+
display.FitAll()
79+
start_display()
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env python
2+
3+
##Copyright 2009-2015 Jelle Ferina ([email protected])
4+
##
5+
##This file is part of pythonOCC.
6+
##
7+
##pythonOCC is free software: you can redistribute it and/or modify
8+
##it under the terms of the GNU Lesser General Public License as published by
9+
##the Free Software Foundation, either version 3 of the License, or
10+
##(at your option) any later version.
11+
##
12+
##pythonOCC is distributed in the hope that it will be useful,
13+
##but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
##GNU Lesser General Public License for more details.
16+
##
17+
##You should have received a copy of the GNU Lesser General Public License
18+
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
19+
20+
from __future__ import print_function
21+
22+
import math
23+
import time
24+
import sys
25+
26+
from OCC.gp import gp_Pnt2d, gp_Pln
27+
from OCC.Geom import Geom_Plane
28+
from OCC.FairCurve import FairCurve_MinimalVariation
29+
from OCC.BRepBuilderAPI import BRepBuilderAPI_MakeEdge
30+
from OCC.Display.SimpleGui import init_display
31+
display, start_display, add_menu, add_function_to_menu = init_display()
32+
33+
34+
def error_code(n):
35+
errors = {0: "FairCurve_OK",
36+
1: "FairCurve_NotConverged",
37+
2: "FairCurve_InfiniteSliding",
38+
3: "FairCurve_NullHeight",
39+
}
40+
return errors[n]
41+
42+
43+
def batten_curve(pt1, pt2, height, slope, angle1, angle2):
44+
fc = FairCurve_MinimalVariation(pt1, pt2, height, slope)
45+
fc.SetConstraintOrder1(2)
46+
fc.SetConstraintOrder2(2)
47+
fc.SetAngle1(angle1)
48+
fc.SetAngle2(angle2)
49+
fc.SetHeight(height)
50+
fc.SetSlope(slope)
51+
fc.SetFreeSliding(True)
52+
print(fc.DumpToString())
53+
status = fc.Compute()
54+
print(error_code(status[0]), error_code(status[1]))
55+
return fc.Curve()
56+
57+
58+
def faircurve(event=None):
59+
pt1 = gp_Pnt2d(0., 0.)
60+
pt2 = gp_Pnt2d(0., 120.)
61+
height = 100.
62+
pl = Geom_Plane(gp_Pln())
63+
for i in range(0, 40):
64+
# TODO: the parameter slope needs to be visualized
65+
slope = i/100.
66+
bc = batten_curve(pt1, pt2, height, slope,
67+
math.radians(i), math.radians(-i))
68+
display.EraseAll()
69+
edge = BRepBuilderAPI_MakeEdge(bc, pl.GetHandle()).Edge()
70+
display.DisplayShape(edge, update=True)
71+
time.sleep(0.21)
72+
73+
74+
def exit(event=None):
75+
sys.exit(0)
76+
77+
if __name__ == "__main__":
78+
add_menu('fair curve')
79+
add_function_to_menu('fair curve', faircurve)
80+
add_function_to_menu('fair curve', exit)
81+
start_display()

0 commit comments

Comments
 (0)