New Part Design Tutorial for FC 019 and 020

A place to share learning material: written tutorials, videos, etc.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
edwilliams16
Veteran
Posts: 3108
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: New Part Design Tutorial for FC 019 and 020

Post by edwilliams16 »

@onekk

This is more your line of expertise, but I wonder if it is worth linking this tutorial to pages with scripts that will create the object?

This purely topological:

Code: Select all

import Part
from FreeCAD import Vector as V3

doc = App.ActiveDocument
length = 53
width = 26
height = 26
topoffset = 5
notchdepth = 5
notchlength = 11
side_height = 16.7
side_length = 7
holelength = 17
holeslantbtm = 7
holeslantheight = 17

#side profile
p1 = V3(0, 0, 0) #origin
p2 = V3(0, 0, height)
p3 = V3(0, -topoffset, height)
p4 = V3(0, -width, 0)
side = Part.makePolygon([p1, p2, p3, p4, p1])
block = Part.Face(side).extrude(V3(length/2, 0 ,0))
#Part.show(block)

#side rectangles
s1 = V3(length/2, 0, 0)
s2 = V3(length/2, 0, side_height)
s3 = V3(length/2, -width, side_height)
s4 = V3(length/2, -width, 0)
side1 = Part.makePolygon([s1, s2, s3, s4, s1])
side1block = Part.Face(side1).extrude(V3(-side_length, 0, 0))
#Part.show(side1block)

n1 = V3(length/2, 0, height)
n2 = n1 - V3(notchlength, 0, 0)
n3 = n2 - V3(0, 0, notchdepth)
n4 = n1 - V3(0, 0, notchdepth)
side2 = Part.makePolygon([n1, n2, n3, n4, n1])
side2block = Part.Face(side2).extrude(V3(0, -width, 0))
#Part.show(side2block)

slantdir = (p3 - p4).normalize()
h1 = p4 + holeslantbtm * slantdir
h2 = h1 + holeslantheight * slantdir
h3 = h2 - V3(0, h2.y, 0)
h4 = h1 - V3(0, h1.y, 0)
side3 = Part.makePolygon([h1, h2, h3, h4, h1])
side3block = Part.Face(side3).extrude(V3(holelength/2, 0, 0))
#Part.show(side3block)

halffinal = (block.fuse(side1block)).cut(side2block.fuse(side3block)) #combine pieces
reflx = App.Matrix()
reflx.scale(-1, 1, 1) #reflection in YZ plane
reflected =halffinal.transformGeometry(reflx)
final = halffinal.fuse(reflected)
Part.show(final,"PDTutorial")
This creates a parametric feature python object. (Basically the same code with a feature python wrapper.

Code: Select all

import Part, FreeCAD, FreeCADGui
from FreeCAD import Vector as V3
from FreeCAD import Units
import pivy
from pivy import coin

class PDTutorial:
    def __init__(self, obj):
        self.Type = 'PDTutorialObject'
        obj.addProperty("App::PropertyLength","Length","PDTutorial","Length of the object").Length = 53
        obj.addProperty("App::PropertyLength","Width","PDTutorial","Width of the object").Width = 26
        obj.addProperty("App::PropertyLength","Height","PDTutorial","Height of the object").Height = 26
        obj.addProperty("App::PropertyLength","TopWidth","PDTutorial","Width of the object top").TopWidth = 5
        obj.addProperty("App::PropertyLength","NotchDepth","PDTutorial","Depth of top corner notch").NotchDepth = 5
        obj.addProperty("App::PropertyLength","NotchLength","PDTutorial","Length of top corner notch").NotchLength = 11
        obj.addProperty("App::PropertyLength","SideHeight","PDTutorial","Height of side block").SideHeight = 16.7
        obj.addProperty("App::PropertyLength","SideLength","PDTutorial","Length of side block").SideLength = 7
        obj.addProperty("App::PropertyLength","HoleLength","PDTutorial","Length of central hole").HoleLength = 17
        obj.addProperty("App::PropertyLength","HoleSlantBottom","PDTutorial","Central hole from bottom edge").HoleSlantBottom = 7
        obj.addProperty("App::PropertyLength","HoleSlantHeight","PDTutorial","Slant height of central hole").HoleSlantHeight = 17

        obj.Proxy = self

    def execute(self, fp):
        #doc = App.ActiveDocument
        length = fp.Length
        width = fp.Width
        height = fp.Height
        topoffset = fp.TopWidth
        notchdepth = fp.NotchDepth
        notchlength = fp.NotchLength
        side_height = fp.SideHeight
        side_length = fp.SideLength
        holelength = fp.HoleLength
        holeslantbtm = fp.HoleSlantBottom
        holeslantheight = fp.HoleSlantHeight

        #side profile
        p1 = V3(0, 0, 0) #origin
        p2 = V3(0, 0, height)
        p3 = V3(0, -topoffset, height)
        p4 = V3(0, -width, 0)
        side = Part.makePolygon([p1, p2, p3, p4, p1])
        block = Part.Face(side).extrude(V3(length/2, 0 ,0))
        #Part.show(block)

        #side rectangles
        s1 = V3(length/2, 0, 0)
        s2 = V3(length/2, 0, side_height)
        s3 = V3(length/2, -width, side_height)
        s4 = V3(length/2, -width, 0)
        side1 = Part.makePolygon([s1, s2, s3, s4, s1])
        side1block = Part.Face(side1).extrude(V3(-side_length, 0, 0))
        #Part.show(side1block)

        n1 = V3(length/2, 0, height)
        n2 = n1 - V3(notchlength, 0, 0)
        n3 = n2 - V3(0, 0, notchdepth)
        n4 = n1 - V3(0, 0, notchdepth)
        side2 = Part.makePolygon([n1, n2, n3, n4, n1])
        side2block = Part.Face(side2).extrude(V3(0, -width, 0))
        #Part.show(side2block)

        slantdir = (p3 - p4).normalize()
        h1 = p4 + holeslantbtm.Value * slantdir
        h2 = h1 + holeslantheight.Value * slantdir   
        h3 = h2 - V3(0, h2.y, 0)
        h4 = h1 - V3(0, h1.y, 0)
        side3 = Part.makePolygon([h1, h2, h3, h4, h1])
        side3block = Part.Face(side3).extrude(V3(holelength/2, 0, 0))
        #Part.show(side3block)

        halffinal = (block.fuse(side1block)).cut(side2block.fuse(side3block)) #combine pieces
        reflx = App.Matrix()
        reflx.scale(-1, 1, 1) #reflection in YZ plane
        reflected =halffinal.transformGeometry(reflx)
        final = halffinal.fuse(reflected)
        #Part.show(final,"PDTutorial")
        fp.Shape = final

doc = FreeCAD.ActiveDocument
a=doc.addObject("Part::FeaturePython","PDTutorial")
PDTutorial(a)
a.ViewObject.Proxy = 0
doc.recompute()
It took me a while to use the .Value property to get the scalar * vector operation to work in the latter.
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: New Part Design Tutorial for FC 019 and 020

Post by onekk »

One initial idea was to create a new tutorial that recreates the example shape using PD scripting.

I have already the script, so some minor modifications should be done to bring it in sync with actual example.

https://forum.freecadweb.org/viewtopic.php?f=22&t=72207

Probably we could put togheter another page with your Part WB version too.

I think that it will be a good thing as with the same shape, different approaches will show pros and cons, or simply differences in workflows.

Let me try in the next days to try to put together a skeleton page for the PD scripting part.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
edwilliams16
Veteran
Posts: 3108
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: New Part Design Tutorial for FC 019 and 020

Post by edwilliams16 »

Take your pick on script versions. Whatever you choose, I think it is helpful to show how easily you can make a scripted object wrapper, so that you change change parameters without messing with the script.
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: New Part Design Tutorial for FC 019 and 020

Post by onekk »

edwilliams16 wrote: Sat Nov 19, 2022 3:39 am ... I think it is helpful to show how easily you can make a scripted object wrapper, so that you change change parameters without messing with the script.
Sorry but I can't guess the sense, probably I'm misunderstanding "the wrapper thing".

However, more decent tutorials, we should be ale to do, less work on forum, or better less "repetition of already done answers".

Sadly my math ability is very low so I can't make some decent tutorials, and as you know, even my knowledge is flawed, due to my "math impairing" :lol: .

Many thanks for your "long proven" kindness and patience in trying to teach me complicated things. :D

Kind Regards.

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: New Part Design Tutorial for FC 019 and 020

Post by Roy_043 »

Challenge for Xmas:
The sketch for the center hole is susceptible to flipping. If you change the width in the master sketch from 26 mm to 50 mm and back you end up with the result in the image. Is there a way to avoid this?
Attachments
pd_tut_019_flipping.png
pd_tut_019_flipping.png (5.27 KiB) Viewed 2126 times
chrisb
Veteran
Posts: 53930
Joined: Tue Mar 17, 2015 9:14 am

Re: New Part Design Tutorial for FC 019 and 020

Post by chrisb »

Roy_043 wrote: Sun Dec 25, 2022 10:49 am Challenge for Xmas:
File? So we start from the same starting point.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: New Part Design Tutorial for FC 019 and 020

Post by Roy_043 »

Right. :mrgreen:
Attachments
pd_tut_019_roy.FCStd
(55.67 KiB) Downloaded 53 times
User avatar
Shalmeneser
Veteran
Posts: 9475
Joined: Wed Dec 23, 2020 12:04 am
Location: Fr

Re: New Part Design Tutorial for FC 019 and 020

Post by Shalmeneser »

:?:
Attachments
Capture d’écran 2022-12-25 180728.jpg
Capture d’écran 2022-12-25 180728.jpg (24.58 KiB) Viewed 2045 times
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: New Part Design Tutorial for FC 019 and 020

Post by Roy_043 »

That solution mitigates the problem, but there are still cases where flipping occurs: try changing the width from 26 mm to 90 mm and back.
User avatar
Shalmeneser
Veteran
Posts: 9475
Joined: Wed Dec 23, 2020 12:04 am
Location: Fr

Re: New Part Design Tutorial for FC 019 and 020

Post by Shalmeneser »

The point between L1 and L2 is not constrained on the red line.
AC=AB+BC ⇔ B ∈ [AC].
Attachments
Capture d’écran 2022-12-26 123812.jpg
Capture d’écran 2022-12-26 123812.jpg (38.07 KiB) Viewed 1958 times
pd_tut_019_roy_SHALM2.FCStd
(45.18 KiB) Downloaded 56 times
Post Reply