Looking for some helpful GUI-commands

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
User avatar
microelly2
Posts: 4690
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Looking for some helpful GUI-commands

Postby microelly2 » Mon Jul 14, 2014 2:49 pm

To simplify the use of FreeCAD for enduser (non python programmers) I'm looking for commands inside the GUI. Before I start to reinvent the wheel some questions.

There is a function in Sketcher WP
"View sketch perpendicular to sketch plane"
I want a simular functionality elsewhere:
view/camera perpendicular to a selected plane face or parallel to a selected edge
Is there already something like this in the GUI? In Draft WP I can set the work plane to the view but not vica versa.

I want to rotate a part along a axis (selected edge or 2 selected points) with a given angle from GUI:
select part
select axis
run command
command input dialog: angle

(there is the rotate command GUI in Draft - but its not the same - the axis is there the normal to the work plane)

thanks for a hint.
wmayer
Site Admin
Posts: 16866
Joined: Thu Feb 19, 2009 10:32 am

Re: Looking for some helpful GUI-commands

Postby wmayer » Mon Jul 14, 2014 3:47 pm

Here an example how the basic stuff works. Create a box and select its Face4

Code: Select all

v=App.ActiveDocument.ActiveObject.Shape.Face4.Surface.Axis
r=App.Rotation(App.Vector(0,0,1),v)
Gui.ActiveDocument.ActiveView.setCameraOrientation(r.Q)
The basic idea is to calculate the axis direction (here the plane normal) and then calculate the quaternion and pass the Q values to the setCameraOrientation method.
User avatar
yorik
Site Admin
Posts: 12146
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels, Belgium
Contact:

Re: Looking for some helpful GUI-commands

Postby yorik » Mon Jul 14, 2014 3:53 pm

There is also a macro for that already: Macro Align View to Face

Wow, I had never realized that the App.Rotation could calculate on the fly the rotation between a vector and another! Tremendously useful...
User avatar
microelly2
Posts: 4690
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: Looking for some helpful GUI-commands

Postby microelly2 » Mon Jul 14, 2014 6:11 pm

thank you werner and yorik,

here is my macro based on the tipp of werner. it works on two points

Code: Select all

# select 2 points
# run the macro
# the camera now ist directed from point 2 to point 1

def _alignCamera(sub0,sub1,reverse=False):
	if reverse:
		v=sub0.sub(sub1)
	else:
		v=sub1.sub(sub0)
	print(v)
	r=App.Rotation(App.Vector(0,0,1),v)
	print(r)
	FreeCADGui.ActiveDocument.ActiveView.setCameraOrientation(r.Q)

def alignCamera():
   MouseSel = Gui.Selection.getSelectionEx()
   Selected_Points = []
   for i in range(len(MouseSel)):
      Sel_i_Object = MouseSel[i]
      SubObjects_Inside = Sel_i_Object.SubObjects
      for n in range(len(SubObjects_Inside)):
        SubObject = SubObjects_Inside[n]
        if SubObject.ShapeType == "Vertex":
          Selected_Points.append(SubObject)
   Number_of_Points = len(Selected_Points)
   print Number_of_Points
   if Number_of_Points == 2:
      print "Selected Points", Selected_Points
      _alignCamera(Selected_Points[0].Point,Selected_Points[1].Point,False)
   else:
      FreeCAD.Console.PrintError("Select 2 Points!")

alignCamera()
The macro of jonathan works fine too.
I will extend it to make the camera parallel to a selected edge.
I think/hope this will be enough to build wood scaffoldings for regular polyhedrons with draft WBwith less calculations:
Inspired by this viewtopic.php?f=3&t=6979