App.Rotate with cylinders

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
fjod
Posts: 14
Joined: Fri Jan 19, 2018 12:01 pm

App.Rotate with cylinders

Post by fjod »

Please send help.

Ok here is the test script I wrote just to check if I have not gone mad.

Code: Select all

import sys

sys.path.append("C:\\Program Files\\FreeCAD 0.17\\bin")
sys.path.append("C:\\Program Files\\FreeCAD 0.17\\lib")

import FreeCAD as App

import FreeCAD, FreeCADGui
FreeCADGui.setupWithoutGUI()
import Mesh
document = FreeCAD.newDocument("testDoc")
stvolikObj = App.ActiveDocument.addObject("Part::Cylinder", "Outer")
stvolikObj.Radius = str(float(8)/2) + ' mm'
stvolikObj.Height = str(float(30) + 2) + ' mm'

for number in range(5):
    box = App.ActiveDocument.addObject("Part::Cylinder", "SuvCyl" + str(number))
    box.Radius = str(float(4)) + ' mm'
    box.Placement = App.Placement(App.Vector(-5, 7.5, number*3), App.Rotation(App.Vector(0, 1, 0), 90))

    cut = App.ActiveDocument.addObject("Part::Cut","ToolCut" + str(number))

    angle = 0
    if (number > 0):
        angle = 10
    stvolikObj.Placement = App.Placement(App.Vector(0, 0, 0), App.Rotation(App.Vector(0, 0, 1), angle))

    cut.Base = stvolikObj
    cut.Tool = box
    App.ActiveDocument.recompute()
    stvolikObj = FreeCAD.ActiveDocument.Objects[-1]

exportFile = "F:\jkhh1.stl"

_objs = []
_objs.append(FreeCAD.ActiveDocument.Objects[-1])
Mesh.export(_objs,exportFile.strip())
Basically it creates one "main" cylinder and tries to cut from it several small cylinders during loop. Cuts start from the bottom of main cylinder, first cut angle = 0. The problem is, the angle of first cut depends on the angle of next one, or last one, or whatever it decides.

angle = 10 https://imgur.com/a/A5z29Ix
angle = 20 https://imgur.com/a/Q7EYyGx
angle = 30 https://imgur.com/a/LSkPs0Z

It's like FreeCAD places last cut nicely (I used "view from behind (4)), so last cut always faces screen. But I need such behavior for first one.


OS: Windows 10
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.17.13541 (Git)
Build type: Release
Branch: releases/FreeCAD-0-17
Hash: 9948ee4f1570df9216862a79705afb367b2c6ffb
Python version: 2.7.14
Qt version: 4.8.7
Coin version: 4.0.0a
OCC version: 7.2.0
Locale: Russian/RussianFederation (ru_RU)
chrisb
Veteran
Posts: 54150
Joined: Tue Mar 17, 2015 9:14 am

Re: App.Rotate with cylinders

Post by chrisb »

Moved to Python forum.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
fjod
Posts: 14
Joined: Fri Jan 19, 2018 12:01 pm

Re: App.Rotate with cylinders

Post by fjod »

chrisb wrote: Mon Dec 16, 2019 4:10 pm Moved to Python forum.
python is working as expected here :P
fjod
Posts: 14
Joined: Fri Jan 19, 2018 12:01 pm

Re: App.Rotate with cylinders

Post by fjod »

I tried to add cube to make my problem more clear

Code: Select all

import sys

sys.path.append("C:\\Program Files\\FreeCAD 0.17\\bin")
sys.path.append("C:\\Program Files\\FreeCAD 0.17\\lib")
import FreeCAD as App

import FreeCAD, FreeCADGui
FreeCADGui.setupWithoutGUI()
import Mesh
document = FreeCAD.newDocument("testDoc")
stvolikObj = App.ActiveDocument.addObject("Part::Cylinder", "Outer")
stvolikObj.Radius = str(float(8)/2) + ' mm'
stvolikObj.Height = str(float(30) + 2) + ' mm'

box3 = App.ActiveDocument.addObject("Part::Box", "TempBox")
box3.Width = str(5) + ' mm'
box3.Height = str(5) + ' mm'
box3.Length = '5  mm'
box3.Placement = App.Placement(App.Vector(0, 0, 20), App.Rotation(App.Vector(0, 1, 0), 0))


fus = App.ActiveDocument.addObject("Part::Fuse", "Fusion2")
fus.Base = stvolikObj
fus.Tool = box3

stvolikObj = fus

App.ActiveDocument.recompute()

for number in range(5):
    box = App.ActiveDocument.addObject("Part::Cylinder", "SuvCyl" + str(number))
    box.Radius = str(float(4)) + ' mm'
    box.Placement = App.Placement(App.Vector(-5, 7.5, number*3), App.Rotation(App.Vector(0, 1, 0), 90))

    cut = App.ActiveDocument.addObject("Part::Cut","ToolCut" + str(number))

    angle = 0
    if (number > 0):
        angle = 30
    stvolikObj.Placement = App.Placement(App.Vector(0, 0, 0), App.Rotation(App.Vector(0, 0, 1), angle))

    cut.Base = stvolikObj
    cut.Tool = box
    App.ActiveDocument.recompute()
    stvolikObj = FreeCAD.ActiveDocument.Objects[-1]

exportFile = "F:\jkhh1.stl"

_objs = []
_objs.append(FreeCAD.ActiveDocument.Objects[-1])
Mesh.export(_objs,exportFile.strip())
angle = 10 https://imgur.com/a/GFvRwrs
angle = 20 https://imgur.com/a/WM1HVsJ
angle = 30 https://imgur.com/a/lduXdbm

pay attention to axis in the right-down corner; it is different for each model, should be same.
fjod
Posts: 14
Joined: Fri Jan 19, 2018 12:01 pm

Re: App.Rotate with cylinders

Post by fjod »

ok I got it, I need to rotate it back on the sum of degrees
import sys

sys.path.append("C:\\Program Files\\FreeCAD 0.17\\bin")
sys.path.append("C:\\Program Files\\FreeCAD 0.17\\lib")


import FreeCAD as App

import FreeCAD, FreeCADGui
FreeCADGui.setupWithoutGUI()
import Mesh
document = FreeCAD.newDocument("testDoc")
stvolikObj = App.ActiveDocument.addObject("Part::Cylinder", "Outer")
stvolikObj.Radius = str(float(8)/2) + ' mm'
stvolikObj.Height = str(float(30) + 2) + ' mm'

box3 = App.ActiveDocument.addObject("Part::Box", "TempBox")
box3.Width = str(5) + ' mm'
box3.Height = str(5) + ' mm'
box3.Length = '5 mm'
box3.Placement = App.Placement(App.Vector(0, 0, 20), App.Rotation(App.Vector(0, 1, 0), 0))


#fus = App.ActiveDocument.addObject("Part::Fuse", "Fusion2")
#fus.Base = stvolikObj
#fus.Tool = box3

#stvolikObj = fus

App.ActiveDocument.recompute()

angleVar = 30

for number in range(5):
box = App.ActiveDocument.addObject("Part::Cylinder", "SuvCyl" + str(number))
box.Radius = str(float(4)) + ' mm'
box.Placement = App.Placement(App.Vector(-5, 7.5, number*3), App.Rotation(App.Vector(0, 1, 0), 90))

cut = App.ActiveDocument.addObject("Part::Cut","ToolCut" + str(number))

angle = 0
if (number > 0):
angle = angleVar
stvolikObj.Placement = App.Placement(App.Vector(0, 0, 0), App.Rotation(App.Vector(0, 0, 1), angle))

cut.Base = stvolikObj
cut.Tool = box
App.ActiveDocument.recompute()
stvolikObj = FreeCAD.ActiveDocument.Objects[-1]

stvolikObj.Placement = App.Placement(App.Vector(0, 0, 0), App.Rotation(App.Vector(0, 0, 1), -4*angleVar))


exportFile = "F:\jkhh1.stl"

_objs = []
_objs.append(FreeCAD.ActiveDocument.Objects[-1])
Mesh.export(_objs,exportFile.strip())
fjod
Posts: 14
Joined: Fri Jan 19, 2018 12:01 pm

Re: App.Rotate with cylinders

Post by fjod »

I'll post an explanation if someone will get stuck at this like me.
Cylinders are rotated as well as other things. But after cut/fuse it becomes new object with rotation = 0, so if one has several rotations, this person needs to calculate sum of angles and rotate result model back on it.

Sorry for some drama :D
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: App.Rotate with cylinders

Post by vocx »

fjod wrote: Tue Dec 17, 2019 1:59 pm I'll post an explanation if someone will get stuck at this like me.
...
Use code tags, not quote tags.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
Post Reply