Circle as custom style for a sensor #752
-
Anyone has a hint on how to achieve this? I managed Solenoids and other 3D models but not a path. Maybe due to the SensorStyle type style attribute? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
hi @BastelBaus sorry for the late reply ... busy times here. Question: What do you mean by Circle as custom style? Do you want to have a circle in the x-y plane ? or do you want to have a circle that is always visible as a circle from the viewpoint - i.e. like a plot marker ? br michael |
Beta Was this translation helpful? Give feedback.
-
Thanks Michael for your efforts I am also so happy that such free communities exist! This works class CoilSensor(magpy.Sensor):
def __init__(self, r=None, position=(0, 0, 0),orientation=None,pixel=None,
handedness="right",style=None,**kwargs,):
self._update()
super().__init__(position,orientation,pixel,handedness,style,**kwargs)
trace = magpy.graphics.model3d.make_CylinderSegment(
dimension=(19/20*r, 21/20*r,r/10, 0, 360),
vert=150)
self.style.model3d.add_trace(trace) # Hand trace over as callable But this not class CoilSensor(magpy.Sensor):
def __init__(self, r=None, position=(0, 0, 0),orientation=None,pixel=None,
handedness="right",style=None,**kwargs,):
self._update()
super().__init__(position,orientation,pixel,handedness,style,**kwargs)
trace = magpy._src.display.traces_core.make_Circle(self)
self.style.model3d.add_trace(trace) # Hand trace over as callable it gives me the following error
Which I understood from debugging a bit, since the properties of SensorStyle and Style does somehow not match and the decoding is somehow "hard coded" (as far as i think I understood ...) BR, |
Beta Was this translation helpful? Give feedback.
-
Hi @BastelBaus, Thanks for your interest in Magpylib ;) As you pointed out, there are only 3D models available in the public API as helper functions. In your code you are using the private API which is not meant to be used that way. There is in fact a rather complex machinery to make this work internally. import magpylib as magpy
import numpy as np
class CoilSensor(magpy.Sensor):
def __init__(
self,
r,
mode="2D",
position=(0, 0, 0),
orientation=None,
pixel=None,
handedness="right",
style=None,
**kwargs,
):
# using keyword arguments since positionals have changed
# from Magpylib v4 to v5. This makes it compatible for both versions.
super().__init__(
position=position,
orientation=orientation,
pixel=pixel,
handedness=handedness,
style=style,
**kwargs,
)
vert = 100
if mode == "3D":
rth = r / 10 # relative thickness
trace = magpy.graphics.model3d.make_CylinderSegment(
dimension=(r - rth / 2, r + rth / 2, rth, 0, 360),
vert=vert,
)
elif mode == "2D":
ts = np.linspace(0, 2 * np.pi, vert)
trace = {
"backend": "generic",
"constructor": "Scatter3d",
"kwargs": {
"x": r * np.cos(ts),
"y": r * np.sin(ts),
"z": np.zeros(vert),
"mode": "lines",
},
}
else:
raise ValueError("Invalid mode, must be either 2D or 3D")
self.style.model3d.add_trace(trace) # trace is not a callable
# Example of usage
cs2D = CoilSensor(r=1, mode="2D", style_label="CoilSensor2D")
cs3D = CoilSensor(r=2, mode="3D", style_label="CoilSensor3D")
coll = magpy.Collection(cs2D, cs3D)
coll.rotate_from_angax([0, 20, 40, 60], "x")
magpy.show(*coll, style_path_frames=1) Note that the code example above does not make use of "trace as callable", and therefore does not allow to simply edit the radius after object instantiation. If you need this feature have a look here |
Beta Was this translation helpful? Give feedback.
Hi @BastelBaus,
Thanks for your interest in Magpylib ;)
As you pointed out, there are only 3D models available in the public API as helper functions. In your code you are using the private API which is not meant to be used that way. There is in fact a rather complex machinery to make this work internally.
There is however a simple way to achieve what you want by defining a custom trace as a dictionary (see also the example in the docs here).