Skip to content

Commit

Permalink
Custom Body
Browse files Browse the repository at this point in the history
  • Loading branch information
mnesarco committed Apr 11, 2020
1 parent 0065135 commit 28f8309
Show file tree
Hide file tree
Showing 6 changed files with 333 additions and 159 deletions.
140 changes: 140 additions & 0 deletions docs/examples/example_custom_body.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 46 additions & 13 deletions src/marz_body_feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
from marz_neck_data import NeckData
from marz_fretboard_data import FretboardData
from marz_threading import Task
from marz_ui import (createPartBody, errorDialog, recomputeActiveDocument,
from marz_ui import (createPartBody, errorDialog, recomputeActiveDocument, Log,
updatePartShape)
from marz_utils import startTimeTrace
from marz_vxy import angleVxy, vxy
from marz_neck_feature import NeckFeature

def createBodyComp(bodyd, height, pos):
def createBodyComp(bodyd, height, pos, topThickness=0, top=False, back=False):
"""Create Body Top or Back
Arguments:
bodyd {BodyData} -- Body's data
Expand All @@ -41,22 +41,55 @@ def createBodyComp(bodyd, height, pos):
{Shape} -- blank
"""

comp = None
angle = deg(bodyd.neckAngle)
contour = App.ActiveDocument.getObject('Marz_Body_Contour')
pos = Vector(pos.x, 0, pos.z)
if contour:
pos = Vector(pos.x, 0, pos.z)
shape = contour.Shape
face = Part.Face(shape.copy())
solid = face.extrude(Vector(0, 0, height))
solid.Placement = Placement(pos, Rotation(Vector(0,1,0), -bodyd.neckAngle))
return solid
comp = solid
else:
a = Vector(0, 0, 0)
b = Vector(-height*math.sin(angle), 0, height*math.cos(angle))
d = Vector(-bodyd.length*math.cos(angle), 0, -bodyd.length*math.sin(angle))
c = Vector(d.x, d.y, d.z).add(b)
points = [p.add(pos) for p in [a,b,c,d,a]]
return Part.Face(Part.makePolygon(points)).extrude(Vector(0, bodyd.width, 0))
w2 = bodyd.width/2
h = bodyd.length
points = [
Vector(0,w2,0), Vector(-h,w2,0), Vector(-h,-w2,0), Vector(0,-w2,0), Vector(0,w2,0)
]
comp = Part.Face( Part.makePolygon(points) ).extrude(Vector(0, 0, height))

pockets = App.ActiveDocument.getObject('Marz_Body_Pockets')
if pockets:
shape = pockets.Shape.copy()
shape.translate(Vector(0,0,height + topThickness))
try:
comp = comp.cut(shape)
except:
Log(f'Ignoring some pockets: {pockets.Name}')

if top:
pockets = App.ActiveDocument.getObject('Marz_Body_Pockets_Top')
if pockets:
shape = pockets.Shape.copy()
shape.translate(Vector(0,0,height))
try:
comp = comp.cut(shape)
except:
Log(f'Ignoring some pockets: {pockets.Name}')

if back:
pockets = App.ActiveDocument.getObject('Marz_Body_Pockets_Back')
if pockets:
shape = pockets.Shape.copy()
shape.translate(Vector(0,0,height))
try:
comp = comp.cut(shape)
except:
Log(f'Ignoring some pockets: {pockets.Name}')

comp.Placement = Placement(pos, Rotation(Vector(0,1,0), -bodyd.neckAngle))

return comp

def blanks(inst, bodyd):

Expand All @@ -66,10 +99,10 @@ def blanks(inst, bodyd):
b = Vector(x,y,0)

b = b.add(Vector(bodyd.totalThicknessWithOffset()*math.sin(angle), 0, -bodyd.totalThicknessWithOffset()*math.cos(angle)))
back = createBodyComp(bodyd, bodyd.backThickness, b)
back = createBodyComp(bodyd, bodyd.backThickness, b, bodyd.topThickness, back=True)

t = Vector(b.x - bodyd.backThickness*math.sin(angle), y, b.z + bodyd.backThickness*math.cos(angle))
top = createBodyComp(bodyd, bodyd.topThickness, t)
top = createBodyComp(bodyd, bodyd.topThickness, t, 0, top=True)

# Pocket
heel = NeckFeature(inst).heel(bodyd.neckd, bodyd.neckd.fbd.neckFrame.midLine)
Expand Down
60 changes: 3 additions & 57 deletions src/marz_cmd_import_body.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,62 +19,7 @@
import marz_ui
from marz_instrument_feature import MarzInstrument
import marz_utils

def importBodyShape(filename):

# Defered Imports to speedup Workbench activation
import importSVG
from FreeCAD import Part, Vector
import marz_geom as geom

# Save Working doc
workingDoc = App.ActiveDocument

# Import SVG File
name = marz_utils.randomString(16)
importSVG.insert(filename, name)
doc = App.getDocument(name)

# Find contour and midline by id
contour = None
midline = None
for obj in doc.Objects:
if obj.Name == 'contour':
contour = obj
elif obj.Name == 'midline':
midline = obj

if not contour:
marz_ui.errorDialog('The SVG File does not contain any contour path. Make sure you have a path with id=contour')
return

if not midline:
marz_ui.errorDialog('The SVG File does not contain any midline path. Make sure you have a path with id=midline')
return

# Find contour and midline intersection
(d, vs, es) = midline.Shape.distToShape( contour.Shape )
neckAnchor = vs[0][0]

if d > 0.0000001:
marz_ui.errorDialog('contour path and midline path must intersect where the neck will be anchored')
return

# Copy Shapes and Upgrade Paths to Wires
wcontour = Part.Wire( contour.Shape.copy().Edges )

# Restore Active Doc
App.setActiveDocument(workingDoc.Name)
App.closeDocument(doc.Name)

# Move contour to correct position
wcontour.translate( -neckAnchor )

# Add contour to document
geom.addOrUpdatePart(wcontour, 'Marz_Body_Contour', 'Body Contour', visibility=False)
App.ActiveDocument.getObject('Marz_Instrument').touch()
App.ActiveDocument.recompute()

from marz_import_svg import extractCustomShape

class CmdImportBodyShape:
"Import body shape from svg Command"
Expand All @@ -97,7 +42,8 @@ def Activated(self):
try:
name = QtGui.QFileDialog.getOpenFileName(QtGui.QApplication.activeWindow(),'Select .svg file','*.svg')[0]
if name:
importBodyShape(name)
#importBodyShape(name)
extractCustomShape(name, 'Marz_Body')
except:
marz_ui.Msg(traceback.format_exc())

Expand Down
Loading

0 comments on commit 28f8309

Please sign in to comment.