FreeCAD-macros

Форк
0
/
SketcherClipView.FCMacro 
118 строк · 3.7 Кб
1
# -*- coding: utf-8 -*-
2

3
###############################################
4
#
5
#	SketcherClipView
6
#
7
# This macro creates a temporary cross section at the
8
# sketch plane in order to 'look into' an object when you
9
# want to create a feature on the inside (i.e. create a revolved
10
# groove on the inner side of a tube).
11
#
12
# The macro creates a clipping plane at the sketch plane and
13
# activates it. Running the macro again deletes the clipping plane.
14
#
15
# If you map the macro to F7 it behaves almost exactly like
16
# in Autodesk Inventor, from which I got the inspiration.
17
#
18
# (c) Ricardo Beck
19
#################################################
20

21
__Name__ = 'Sketcher Clip View'
22
__Comment__ = 'Creates a temporary clipping plane at the support plane of the sketch'
23
__License__ = ''
24
__Web__ = 'https://forum.freecadweb.org/viewtopic.php?t=26608'
25
__Wiki__ = ''
26
__Icon__ = 'SketcherClipView.svg'
27
__Help__ = 'Launch while editing a sketch'
28
__Author__ = 'Ricardo Beck, galou_breizh'
29
__Version__ = '1.1.0'
30
__Date__ = '2018-05-29'
31
__Status__ = 'Beta'
32
__Requires__ = 'FreeCAD >= v0.17'
33
__Files__ = 'SketcherClipView.svg'
34

35
from pivy import coin
36

37
import freecad as fc
38
import FreeCAD as app
39
import FreeCADGui as gui
40
from PySide import QtCore
41

42

43
def in_edit_mode():
44
    """Return True if the Sketcher is in edit mode
45

46
    This is done by checking whether ActiveSketch is defined and
47
    ActiveSketch.ViewObject.TempoVis is not None.
48
    """
49
    in_edit_mode = False
50
    try:
51
        in_edit_mode = (ActiveSketch.ViewObject.TempoVis is not None)
52
    except NameError:
53
        pass
54
    return in_edit_mode
55

56

57
def check_leave_edit():
58
    if not in_edit_mode():
59
        clean()
60

61

62
def clean():
63
    if hasattr(fc, 'SketcherClipView_clip_plane'):
64
        gui.activeDocument().ActiveView.getSceneGraph().removeChild(fc.SketcherClipView_clip_plane)
65
        del fc.SketcherClipView_clip_plane
66
        del fc.SketcherClipView_timer
67

68

69
def main():
70
    # Check if there is a temporary clipping plane from a previous run of the
71
    # macro.
72
    if hasattr(fc, 'SketcherClipView_clip_plane'):
73
        # Clipping plane found, remove from scenegraph and delete the object.
74
        clean()
75
        return
76

77
    if not in_edit_mode():
78
        return
79

80
    # No clipping plane found, create one at the sketch base.
81

82
    # Get the placement information of the active sketch.
83
    try:
84
        mat = ActiveSketch.getGlobalPlacement()
85
    except NameError:
86
        # ActiveSketch undefined (i.e. not in sketch editing mode).
87
        return
88
    point = mat.Base
89
    normal = mat.Rotation.multVec(app.Vector(0, 0, 1))
90

91
    # Create coin3d vectors of the sketch position.
92
    coin_normal_vector = coin.SbVec3f(normal.x, normal.y, normal.z)
93
    coin_normal_vector.negate()
94
    coin_base_point = coin.SbVec3f(point.x, point.y, point.z)
95

96
    # Offset of the clipping plane so the sketch elements (lines, etc) are not cut off.
97
    sketch_offset_factor = -0.02
98

99
    coin_normal_vector_normalized = coin.SbVec3f(coin_normal_vector)
100
    coin_normal_vector_normalized.normalize()
101

102
    # Offset the clipping plane base by a fraction of the (normalized) normal vector.
103
    coin_base_point += (coin_normal_vector_normalized * sketch_offset_factor)
104

105
    # Create the clipping plane at the calculated position.
106
    fc.SketcherClipView_clip_plane = coin.SoClipPlane()
107
    fc.SketcherClipView_clip_plane.plane.setValue(coin.SbPlane(coin_normal_vector,coin_base_point))
108
    gui.activeDocument().ActiveView.getSceneGraph().insertChild(fc.SketcherClipView_clip_plane, 0)
109

110
    # Switch clipping plane on.
111
    fc.SketcherClipView_clip_plane.on.setValue(True)
112

113
    fc.SketcherClipView_timer = QtCore.QTimer()
114
    fc.SketcherClipView_timer.timeout.connect(check_leave_edit)
115
    fc.SketcherClipView_timer.start(100)  # Pool every 100 ms.
116

117
if __name__ == '__main__':
118
    main()
119

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.