Skip to content

Commit

Permalink
Merge pull request FreeCAD#10767 from qewer33/assembly-jcs
Browse files Browse the repository at this point in the history
Assembly: Improve JCS appearance and implement autoscale
  • Loading branch information
chennes authored Sep 23, 2023
2 parents 1893ff0 + 62722aa commit c3b33f6
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 26 deletions.
86 changes: 60 additions & 26 deletions src/Mod/Assembly/JointObject.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
# *
# ***************************************************************************/

import math

import FreeCAD as App
import Part

Expand Down Expand Up @@ -238,31 +240,23 @@ def findPlacement(self, obj, elt, vtx):
class ViewProviderJoint:
def __init__(self, obj, app_obj):
"""Set this object to the proxy object of the actual view provider"""
obj.addProperty(
"App::PropertyColor", "color_X_axis", "JCS", "Joint coordinate system X axis color"
)
obj.addProperty(
"App::PropertyColor", "color_Y_axis", "JCS", "Joint coordinate system Y axis color"
)
obj.addProperty(
"App::PropertyColor", "color_Z_axis", "JCS", "Joint coordinate system Z axis color"
)
obj.addProperty(
"App::PropertyInteger",
"axis_thickness",
"JCS",
"Joint cordinate system X axis thickness",
)
obj.color_X_axis = (1.0, 0.0, 0.0)
obj.color_Y_axis = (0.0, 1.0, 0.0)
obj.color_Z_axis = (0.0, 0.0, 1.0)
obj.axis_thickness = 2
self.axis_thickness = 3

view_params = App.ParamGet("User parameter:BaseApp/Preferences/View")
param_x_axis_color = view_params.GetUnsigned("AxisXColor", 0xCC333300)
param_y_axis_color = view_params.GetUnsigned("AxisYColor", 0x33CC3300)
param_z_axis_color = view_params.GetUnsigned("AxisZColor", 0x3333CC00)

self.x_axis_so_color = coin.SoBaseColor()
self.x_axis_so_color.rgb.setValue(1.0, 0.0, 0.0)
self.x_axis_so_color.rgb.setValue(UtilsAssembly.color_from_unsigned(param_x_axis_color))
self.y_axis_so_color = coin.SoBaseColor()
self.y_axis_so_color.rgb.setValue(0.0, 1.0, 0.0)
self.y_axis_so_color.rgb.setValue(UtilsAssembly.color_from_unsigned(param_y_axis_color))
self.z_axis_so_color = coin.SoBaseColor()
self.z_axis_so_color.rgb.setValue(0.0, 0.0, 1.0)
self.z_axis_so_color.rgb.setValue(UtilsAssembly.color_from_unsigned(param_z_axis_color))

camera = Gui.ActiveDocument.ActiveView.getCameraNode()
self.cameraSensor = coin.SoFieldSensor(self.camera_callback, camera)
self.cameraSensor.attach(camera.height)

self.app_obj = app_obj
obj.Proxy = self
Expand All @@ -279,7 +273,7 @@ def attach(self, obj):

self.draw_style = coin.SoDrawStyle()
self.draw_style.style = coin.SoDrawStyle.LINES
self.draw_style.lineWidth = obj.axis_thickness
self.draw_style.lineWidth = self.axis_thickness

self.switch_JCS1 = self.JCS_sep(obj, self.transform1)
self.switch_JCS2 = self.JCS_sep(obj, self.transform2)
Expand All @@ -291,6 +285,10 @@ def attach(self, obj):
self.display_mode.addChild(self.switch_JCS_preview)
obj.addDisplayMode(self.display_mode, "Wireframe")

def camera_callback(self, *args):
scaleF = self.get_JCS_size()
self.axisScale.scaleFactor.setValue(scaleF, scaleF, scaleF)

def JCS_sep(self, obj, soTransform):
pick = coin.SoPickStyle()
pick.style.setValue(coin.SoPickStyle.UNPICKABLE)
Expand All @@ -299,10 +297,12 @@ def JCS_sep(self, obj, soTransform):
JCS.addChild(soTransform)
JCS.addChild(pick)

X_axis_sep = self.line_sep([0, 0, 0], [1, 0, 0], self.x_axis_so_color)
Y_axis_sep = self.line_sep([0, 0, 0], [0, 1, 0], self.y_axis_so_color)
base_plane_sep = self.plane_sep(0.4, 15)
X_axis_sep = self.line_sep([0.5, 0, 0], [1, 0, 0], self.x_axis_so_color)
Y_axis_sep = self.line_sep([0, 0.5, 0], [0, 1, 0], self.y_axis_so_color)
Z_axis_sep = self.line_sep([0, 0, 0], [0, 0, 1], self.z_axis_so_color)

JCS.addChild(base_plane_sep)
JCS.addChild(X_axis_sep)
JCS.addChild(Y_axis_sep)
JCS.addChild(Z_axis_sep)
Expand All @@ -326,12 +326,46 @@ def line_sep(self, startPoint, endPoint, soColor):
axis_sep.addChild(line)
return axis_sep

def plane_sep(self, size, num_vertices):
coords = coin.SoCoordinate3()

for i in range(num_vertices):
angle = float(i) / num_vertices * 2.0 * math.pi
x = math.cos(angle) * size
y = math.sin(angle) * size
coords.point.set1Value(i, x, y, 0)

face = coin.SoFaceSet()
face.numVertices.setValue(num_vertices)

transform = coin.SoTransform()
transform.translation.setValue(0, 0, 0)

draw_style = coin.SoDrawStyle()
draw_style.style = coin.SoDrawStyle.FILLED

material = coin.SoMaterial()
material.diffuseColor.setValue([1, 1, 1])
material.ambientColor.setValue([1, 1, 1])
material.specularColor.setValue([1, 1, 1])
material.emissiveColor.setValue([1, 1, 1])
material.transparency.setValue(0.7)

face_sep = coin.SoAnnotation()
face_sep.addChild(self.axisScale)
face_sep.addChild(transform)
face_sep.addChild(draw_style)
face_sep.addChild(material)
face_sep.addChild(coords)
face_sep.addChild(face)
return face_sep

def get_JCS_size(self):
camera = Gui.ActiveDocument.ActiveView.getCameraNode()
if not camera:
return 10

return camera.height.getValue() / 10
return camera.height.getValue() / 20

def set_JCS_placement(self, soTransform, placement):
t = placement.Base
Expand Down
8 changes: 8 additions & 0 deletions src/Mod/Assembly/UtilsAssembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,11 @@ def findVertexNameInObject(vertex, obj):
if vtx.Point == vertex.Point:
return "Vertex" + str(i + 1)
return ""


def color_from_unsigned(c):
return [
float(int((c >> 24) & 0xFF) / 255),
float(int((c >> 16) & 0xFF) / 255),
float(int((c >> 8) & 0xFF) / 255),
]

0 comments on commit c3b33f6

Please sign in to comment.