Code: Select all
# -*- coding: utf-8 -*-
import FreeCADGui
import FreeCAD
from FreeCAD import Base
import Part
import math
class Bolt:
def __init__(self, name):
self.name = name
self.obj = None
def makeBolt(self):
bolt=FreeCAD.ActiveDocument.addObject("Part::FeaturePython", self.name)
bolt.addProperty("App::PropertyLength","d","Bolt","Nominal diameter").d=10.0
bolt.addProperty("App::PropertyLength","L","Bolt","Length of the bolt").L=50.0
bolt.addProperty("App::PropertyLength","K","Bolt","Height of the head of the bolt").K=6.4
bolt.addProperty("App::PropertyLength","S","Bolt","Width of the head of the nut").S=17.0
bolt.addProperty("App::PropertyLength","m","Bolt","Height of the nut").m=8.0
bolt.addProperty("App::PropertyLength","DistanceNutFromHead","Bolt","Distance of the nut from the head").DistanceNutFromHead=30.0
bolt.addProperty("App::PropertyLength","WasherDiameterExternal","Bolt","External diameter of washer").WasherDiameterExternal=22.0
bolt.addProperty("App::PropertyLength","WasherDiameterInternal","Bolt","External diameter of washer").WasherDiameterInternal=11.0
bolt.addProperty("App::PropertyLength","WasherThickness","Bolt","Thickness of the washer").WasherThickness=2.0
bolt.addProperty("App::PropertyBool","WasherUp","Bolt","Washer up").WasherUp=True
bolt.addProperty("App::PropertyBool","WasherDown","Bolt","Washer down").WasherDown=True
bolt.addProperty("App::PropertyBool","Accuracy","Bolt","Accuracy").Accuracy=False
self.obj = bolt
ViewProviderBolt(bolt.ViewObject)
FreeCAD.ActiveDocument.recompute()
class ViewProviderBolt:
def __init__(self, obj):
# Set this object to the proxy object of the actual view provider
obj.Proxy = self
def getDefaultDisplayMode(self):
# Return the name of the default display mode. It must be defined in getDisplayModes.
return "Flat Lines"
def updateData(self, fp, prop):
r = (fp.S/2) / math.cos( math.pi/6 )
Solid = Part.makeCylinder( fp.d/2, fp.L )
DT = fp.L
if fp.WasherUp:
DT -= fp.WasherThickness
Solid.translate( Base.Vector(0,0,-DT) )
if fp.K!=0:
vertexes = []
for i in range(0,7):
vertexes.append(Base.Vector( r * math.cos(math.pi/6+i*math.pi/3), r * math.sin(math.pi/6+i*math.pi/3),0))
Shape=Part.makePolygon(vertexes)
Wire = Part.Wire(Shape.Edges)
Face = Part.Face(Wire)
NutSolid = Face.extrude(Base.Vector(0,0,fp.K))
if fp.Accuracy:
vertexes = []
vertexes.append(Base.Vector(fp.S/2,0,fp.K))
xTop = r * 1.1
zTop = fp.K * 1.1
vertexes.append(Base.Vector(fp.S/2,0,zTop))
vertexes.append(Base.Vector(xTop,0,zTop))
vertexes.append(Base.Vector(xTop,0, fp.K-(r-fp.S/2)*math.tan(math.pi/6.0)))
vertexes.append(Base.Vector(r,0, fp.K-(r-fp.S/2)*math.tan(math.pi/6.0)))
vertexes.append(Base.Vector(fp.S/2,0,fp.K))
CutShape = Part.makePolygon(vertexes)
CutWire = Part.Wire(CutShape.Edges)
CutFace = Part.Face(CutWire)
CutSolid = CutFace.revolve(Base.Vector(0,0,0), Base.Vector(0,0,1),360 )
NutSolid = NutSolid.cut(CutSolid)
vertexes = []
vertexes.append(Base.Vector(fp.S/2,0,0))
vertexes.append(Base.Vector(r,0,0))
vertexes.append(Base.Vector(r,0, (r-fp.S/2)*math.tan(math.pi/6.0)))
vertexes.append(Base.Vector(fp.S/2,0,0))
CutShape=Part.makePolygon(vertexes)
CutWire = Part.Wire(CutShape.Edges)
CutFace = Part.Face(CutWire)
CutSolid = CutFace.revolve(Base.Vector(0,0,0), Base.Vector(0,0,1),360 )
NutSolid = NutSolid.cut(CutSolid)
if fp.WasherUp:
NutSolid.translate( Base.Vector(0,0,fp.WasherThickness) )
Solid = Solid.fuse(NutSolid)
if fp.WasherUp:
Washer = Part.makeCylinder( fp.WasherDiameterExternal/2, fp.WasherThickness )
Washer.cut( Part.makeCylinder( fp.WasherDiameterInternal/2, fp.WasherThickness ) )
Solid = Solid.fuse(Washer)
if fp.WasherDown:
Washer = Part.makeCylinder( fp.WasherDiameterExternal/2, fp.WasherThickness )
Washer.cut( Part.makeCylinder( fp.WasherDiameterInternal/2, fp.WasherThickness ) )
Washer.translate( Base.Vector(0,0,-(fp.DistanceNutFromHead+fp.WasherThickness) ) )
Solid = Solid.fuse(Washer)
if fp.m!=0:
vertexes = []
for i in range(0,7):
vertexes.append(Base.Vector( r * math.cos(math.pi/6+i*math.pi/3), r * math.sin(math.pi/6+i*math.pi/3),0))
Shape=Part.makePolygon(vertexes)
Wire = Part.Wire(Shape.Edges)
Face = Part.Face(Wire)
NutSolid = Face.extrude(Base.Vector(0,0,fp.m))
if fp.Accuracy:
vertexes = []
vertexes.append(Base.Vector(fp.S/2,0,fp.m))
vertexes.append(Base.Vector(r,0,fp.m))
vertexes.append(Base.Vector(r,0, fp.m-(r-fp.S/2)*math.tan(math.pi/6.0)))
vertexes.append(Base.Vector(fp.S/2,0,fp.m))
CutShape=Part.makePolygon(vertexes)
CutWire = Part.Wire(CutShape.Edges)
CutFace = Part.Face(CutWire)
CutSolid = CutFace.revolve(Base.Vector(0,0,0), Base.Vector(0,0,1),360 )
NutSolid = NutSolid.cut(CutSolid)
vertexes = []
vertexes.append(Base.Vector(fp.S/2,0,0))
vertexes.append(Base.Vector(r,0,0))
vertexes.append(Base.Vector(r,0, (r-fp.S/2)*math.tan(math.pi/6.0)))
vertexes.append(Base.Vector(fp.S/2,0,0))
CutShape=Part.makePolygon(vertexes)
CutWire = Part.Wire(CutShape.Edges)
CutFace = Part.Face(CutWire)
CutSolid = CutFace.revolve(Base.Vector(0,0,0), Base.Vector(0,0,1),360 )
NutSolid = NutSolid.cut(CutSolid)
DT = fp.DistanceNutFromHead+fp.m
if fp.WasherDown:
DT += fp.WasherThickness
NutSolid.translate( Base.Vector(0,0,-DT ) )
Solid = Solid.fuse(NutSolid)
fp.Shape = Solid
return
Code: Select all
OS: Windows 10 Version 2009
Word size of FreeCAD: 64-bit
Version: 0.20.29177 (Git)
Build type: Release
Branch: releases/FreeCAD-0-20
Hash: 68e337670e227889217652ddac593c93b5e8dc94
Python 3.8.10, Qt 5.15.2, Coin 4.0.1, Vtk 8.2.0, OCC 7.6.2
Locale: Italian/Italy (it_IT)
Installed mods:
* A2plus 0.4.56a
* Curves 0.5.0
* Fasteners 0.3.46
* gears 1.0.0
* la_freecad
* Pyramids-and-Polyhedrons
* SheetMetal 0.2.49
How to solid part create function ?
thanks.
My elementary english from italy.