Please excuse my crude code I am not a programmer and Python is very new to me.
I am drawing a 'seed' hex at the centre point and then create copies horizontally and vertically until their position is outside a fixed radius.
Then I make a second 'seed' hex at the right offset and repeat the process.
I am sure there are more elegant ways of going about it.
Whilst this works and does the job for me, I was wondering whether I can replace the IsInsideTargetArea(TestObject) function with a way whereby I can draw an outline in the GUI and then fill that outline with the hex pattern. In other words, test whether the centre coordinate of the next hex is within that outline just like I am doing now by checking for distance from the centre point.
Code: Select all
import Draft
import math
import string
import time
PI = 3.14159265
Radius = 120
def RefreshScreen(WaitTime = 0.05):
Gui.updateGui()
# Gui.SendMsgToActiveView("ViewFit")
time.sleep(WaitTime)
i = 0
def clearAll():
doc = FreeCAD.ActiveDocument
for obj in doc.Objects:
doc.removeObject(obj.Name )
RefreshScreen(0.01)
def DegToRad (Degrees):
Degrees *= 2* PI
Degrees /= 360
return Degrees
def RadToDeg (Radians):
Radians *= 360;
Radians /= 2 * PI
return Radians
def DistanceFromCentre(x,y):
return math.sqrt(x*x + y*y)
def IsInsideTargetArea(TestObject):
if TestObject.Placement.Base.Length >=Radius:
return False
else:
return True
def HorizontalDistance(n):
return 2*n*(2*HexOutsideRadius + WallThickness) * math.cos(DegToRad(30))
def VerticalDistance(n):
return 2*n*(2*HexOutsideRadius + WallThickness) * math.sin(DegToRad(30))
def CopyHorizontallyToLimit(SeedHex, Direction):
h = Direction
while True:
x = HorizontalDistance(h)
NewHex = Draft.move(SeedHex,FreeCAD.Vector(x,0,0),True)
RefreshScreen()
if not IsInsideTargetArea(NewHex):
print NewHex.Name
FreeCAD.ActiveDocument.removeObject(NewHex.Name)
RefreshScreen()
Gui.SendMsgToActiveView("ViewFit")
break
h+=Direction
def FillSeedHex(SeedHex, Direction):
i = Direction
CopyHorizontallyToLimit(SeedHex,Direction) # extend initial seed hex along x first
while True:
y = VerticalDistance(i)
VerticalHex1 = Draft.move(SeedHex,FreeCAD.Vector(0,y,0),True)
RefreshScreen()
if not IsInsideTargetArea(VerticalHex1):
print VerticalHex1.Name
FreeCAD.ActiveDocument.removeObject(VerticalHex1.Name)
RefreshScreen()
Gui.SendMsgToActiveView("ViewFit")
break
CopyHorizontallyToLimit(VerticalHex1,1) # make horizontal copies util out to limit radius to the right
CopyHorizontallyToLimit(VerticalHex1,-1) # make horizontal copies util out to limit radius to the left
if not IsInsideTargetArea(VerticalHex1):
print VerticalHex1.Name
FreeCAD.ActiveDocument.removeObject(VerticalHex1.Name)
RefreshScreen()
Gui.SendMsgToActiveView("ViewFit")
break
i+=Direction
#----------------------------------------------------------------------------------------------------
clearAll()
StructureRadius = 120
HexOutsideRadius = 9
WallThickness = 4
HexInsideRadius = HexOutsideRadius * math.cos(DegToRad(30))
print
print '--------------------------------------------------------------------'
print
SeedHex1 = Draft.makePolygon(6,HexOutsideRadius,False) # start with seed hexagon at the origin
RefreshScreen()
FillSeedHex(SeedHex1, 1) # copy it horizontally and vertically until the extents of the limiting circle are reached
FillSeedHex(SeedHex1,-1)
#create second seed hexagon at the right distance
SeedHex2 = Draft.move(SeedHex1,FreeCAD.Vector((2*HexOutsideRadius + WallThickness) * math.cos(DegToRad(30)),(2*HexOutsideRadius + WallThickness) * math.sin(DegToRad(30)),0),True)
RefreshScreen()
FillSeedHex(SeedHex2, 1)
FillSeedHex(SeedHex2,-1)