Skip to content

Commit

Permalink
various work including trying ( but failing ) to improve FitArcs
Browse files Browse the repository at this point in the history
  • Loading branch information
danheeks committed Sep 12, 2020
1 parent 4078d86 commit 8ff2f04
Show file tree
Hide file tree
Showing 23 changed files with 505 additions and 291 deletions.
166 changes: 135 additions & 31 deletions App.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def OnMessageBox(error_message):
def OnInputMode():
wx.GetApp().OnInputMode()

def OnPaint():
if wx.GetApp().coordsys_for_P3P != None:
wx.GetApp().RenderCoordSys()

tools = []
save_filter_for_StartPickObjects = 0
save_just_one_for_EndPickObjects = False
Expand All @@ -56,6 +60,10 @@ def __init__(self):
self.bitmap_path = self.cad_dir + '/bitmaps'
self.inMainLoop = False
self.select_mode = SelectMode.SelectMode()
self.coordsys_for_P3P = None
self.paint_registered = False
self.import_file_types = [] # a list of tuples, example entry: ( ['step', 'stp'], 'Step Files' )
self.export_file_types = [] # a list of tuples

save_out = sys.stdout
save_err = sys.stderr
Expand Down Expand Up @@ -121,9 +129,9 @@ def OnInit(self):
def RegisterObjectTypes(self):
HImage.type = cad.RegisterObjectType("Image", CreateImage)
Gear.type = cad.RegisterObjectType("Gear", CreateGear)
global wx_image_extensions
for ext in wx_image_extensions:
cad.RegisterImportFileType(ext, ImportImageFile)
self.RegisterImportFileTypes(wx_image_extensions, 'Picture Files', ImportImageFile)
import Svg
self.RegisterExportFileTypes(['svg'], 'Svg Files', Svg.Export)

def OnNewOrOpen(self, open):
pass
Expand Down Expand Up @@ -214,14 +222,17 @@ def SplitSketch(self):
cad.EndHistory()

def FitArcs(self):
sketch = self.object
sketch.__class__ = cad.Sketch
curve = sketch.GetCurve()
curve.FitArcs()
cad.StartHistory()
cad.DeleteUndoably(self.object)
cad.AddUndoably(cad.NewSketchFromCurve(curve))
cad.EndHistory()
value = self.InputLength('Set tolerance for Fit Arcs', 'tolerance', geom.get_accuracy())
if value != None:
geom.set_accuracy(value)
sketch = self.object
sketch.__class__ = cad.Sketch
curve = sketch.GetCurve()
curve.FitArcs()
cad.StartHistory()
cad.DeleteUndoably(self.object)
cad.AddUndoably(cad.NewSketchFromCurve(curve))
cad.EndHistory()

def CopyUndoably(self, object, copy_with_new_data):
copy_undoable = CopyObjectUndoable(object, copy_with_new_data)
Expand Down Expand Up @@ -678,28 +689,27 @@ def OnSaveAs(self, e):
dialog.CenterOnParent()
if dialog.ShowModal() != wx.ID_CANCEL:
self.OnSaveFilepath(dialog.GetPath())

def GetImportWildcardString(self):
global wx_image_extensions
imageExtStr = ''
imageExtStr2 = ''
for ext in wx_image_extensions:
if imageExtStr:
imageExtStr += ';'
imageExtStr += '*.'
imageExtStr += ext
if imageExtStr2:
imageExtStr2 += ' '
imageExtStr2 += '*.'
imageExtStr2 += ext
return 'Known Files' + ' |*.heeks;*.HEEKS;*.igs;*.IGS;*.iges;*.IGES;*.stp;*.STP;*.step;*.STEP;*.dxf;*.DXF;*.stl' + imageExtStr + '|Heeks files (*.heeks)|*.heeks;*.HEEKS|STL files (*.stl)|*.stl;*.STL|Scalar Vector Graphics files (*.svg)|*.svg;*.SVG|DXF files (*.dxf)|*.dxf;*.DXF|RS274X/Gerber files (*.gbr,*.rs274x)|*.gbr;*.GBR;*.rs274x;*.RS274X;*.pho;*.PHO|Picture files (' + imageExtStr2 + ')|' + imageExtStr
wild_card_string1 = 'Known Files |*.heeks;*.HEEKS;*.dxf;*.DXF;*.stl'
wild_card_string2 = '|Heeks files (*.heeks)|*.heeks;*.HEEKS|STL files (*.stl)|*.stl;*.STL|Scalar Vector Graphics files (*.svg)|*.svg;*.SVG|DXF files (*.dxf)|*.dxf;*.DXF|RS274X/Gerber files (*.gbr,*.rs274x)|*.gbr;*.GBR;*.rs274x;*.RS274X;*.pho;*.PHO'

wild_card_string1, wild_card_string2 = AddToWildcardStrings(wild_card_string1, wild_card_string2, self.import_file_types)

return wild_card_string1 + wild_card_string2

def GetExportWildcardString(self):
return 'Known Files |*.stl;*.dxf;*.cpp;*.py;*.obj|STL files (*.stl)|*.stl|DXF files (*.dxf)|*.dxf|CPP files (*.cpp)|*.cpp|OpenCAMLib python files (*.py)|*.py|Wavefront .obj files (*.obj)|*.obj'
wild_card_string1 = 'Known Files |*.stl;*.dxf;*.cpp;*.py;*.obj'
wild_card_string2 = '|STL files (*.stl)|*.stl|DXF files (*.dxf)|*.dxf|CPP files (*.cpp)|*.cpp|OpenCAMLib python files (*.py)|*.py|Wavefront .obj files (*.obj)|*.obj'

wild_card_string1, wild_card_string2 = AddToWildcardStrings(wild_card_string1, wild_card_string2, self.export_file_types)

return wild_card_string1 + wild_card_string2

def OnImport(self, e):
config = HeeksConfig()
default_directory = config.Read('ImportDirectory', self.GetDefaultDir())

dialog = wx.FileDialog(self.frame, 'Import File', default_directory, '', self.GetImportWildcardString())
dialog.CenterOnParent()

Expand All @@ -711,7 +721,7 @@ def OnImport(self, e):
if self.filepath == None:
dot = filepath.rfind('.')
if dot != -1:
self.filepath = filepath[:dot+1] + '.heeks'
self.filepath = filepath[:dot+1] + 'heeks'
self.frame.SetFrameTitle()
config.Write('ImportDirectory', dialog.GetDirectory())
cad.Repaint()
Expand All @@ -722,6 +732,16 @@ def GetPathSuffix(self, path):
return ''
return path[dot+1:].lower()

def RegisterImportFileTypes(self, suffix_list, description, ImportCallback):
self.import_file_types.append((suffix_list, description))
for suffix in suffix_list:
cad.RegisterImportFileType(suffix, ImportCallback)

def RegisterExportFileTypes(self, suffix_list, description, ExportCallback):
self.export_file_types.append((suffix_list, description))
for suffix in suffix_list:
cad.RegisterExportFileType(suffix, ExportCallback)

def OnExport(self, e):
config = HeeksConfig()
default_directory = config.Read('ExportDirectory', self.GetDefaultDir())
Expand Down Expand Up @@ -1006,12 +1026,80 @@ def OnText(self, e):

def OnDimensioning(self, e):
pass

def RenderCoordSys(self):
p = cad.GetDigitizing().digitized_point.point
if self.coord_render_mode == 0:
self.coordsys_for_P3P.o = p
elif self.coord_render_mode == 1:
if p != self.coordsys_for_P3P.o:
self.coordsys_for_P3P.x = (p - self.coordsys_for_P3P.o).Normalized()
if self.coordsys_for_P3P.x.Dist(self.z_for_P3P) < 0.0000000001 or self.coordsys_for_P3P.x.Dist(-self.z_for_P3P) < 0.0000000001:
self.coordsys_for_P3P.y = self.y_for_P3P ^ self.coordsys_for_P3P.x
else:
self.coordsys_for_P3P.y = self.z_for_P3P ^ self.coordsys_for_P3P.x
elif self.coord_render_mode == 2:
if p != self.coordsys_for_P3P.o:
y = (p - self.coordsys_for_P3P.o).Normalized()
if y.Dist(self.coordsys_for_P3P.x) > 0.0000000001 and y.Dist(-self.coordsys_for_P3P.x) > 0.0000000001:
z = self.coordsys_for_P3P.x ^ y
self.coordsys_for_P3P.y = z ^ self.coordsys_for_P3P.x
self.coordsys_for_P3P.OnGlCommands(False, False, False)

def MakeOriginFromPickPoints(self, coordsys, three_points):
self.coordsys_for_P3P = coordsys
self.y_for_P3P = coordsys.y
self.z_for_P3P = coordsys.x ^ coordsys.y
coordsys.SetVisible(False)
if self.paint_registered == False:
cad.RegisterOnGLCommands(OnPaint)
self.paint_registered = True

self.coord_render_mode = 0

ret = True

if self.PickPosition('Pick the location') == None:
ret = False

if three_points:
self.coord_render_mode = 1

if ret and self.PickPosition('Pick a point on the x-axis') == None:
ret = False

self.coord_render_mode = 2

if ret and self.PickPosition('Pick a point where y > 0') == None:
ret = False

def OnCoordinateSystem(self, e):
pass
self.coordsys_for_P3P = None
coordsys.SetVisible(True)

def OnNewOrigin(self, e):
pass
return ret

def OnSetOriginPoints(self, three_points):
mat = cad.GetDrawMatrix(False)
o = geom.Point3D(0,0,0).Transformed(mat)
x = geom.Point3D(1,0,0).Transformed(mat) - o
y = geom.Point3D(0,1,0).Transformed(mat) - o
new_object = cad.NewCoordinateSystem("Coordinate System", o, x, y)
cad.ClearSelection()
cad.SetInputMode(self.select_mode)

# and pick from three points
result = self.MakeOriginFromPickPoints(new_object, three_points)
if result:
cad.AddUndoably(new_object)
cad.Select(new_object)
else:
cad.DeleteInternalObject(new_object)

def OnSetOrigin3Points(self, e):
self.OnSetOriginPoints(True)

def OnSetOrigin1Point(self, e):
self.OnSetOriginPoints(False)

def OnMoveTranslate(self, e):
from Transform import Translate
Expand Down Expand Up @@ -1109,5 +1197,21 @@ def RollBack(self):
self.object.CopyFrom(self.old_copy)
cad.WasModified(self.object)

def AddToWildcardStrings(wild_card_string1, wild_card_string2, file_types):
for suffix_list, description in file_types:
wild_card_string2 += '|' + description + ' ('
imageExtStr = ''
imageExtStr2 = ''
for suffix in suffix_list:
wild_card_string1 += ';*.'
wild_card_string1 += suffix
if imageExtStr:
imageExtStr += ' '
imageExtStr2 += ';'
imageExtStr += ('*.' + suffix)
imageExtStr2 += ('*.' + suffix)
wild_card_string2 += (imageExtStr + ')|' + imageExtStr2)

return wild_card_string1, wild_card_string2


12 changes: 8 additions & 4 deletions CAD/CoordinateSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1207,14 +1207,18 @@ HeeksObj *CoordinateSystem::MakeACopy(void)const
void CoordinateSystem::Transform(const Matrix &m)
{
m_o = m_o.Transformed(m);
m_x = m_x.Transformed(m);
m_y = m_y.Transformed(m);
m_x = m_x.TransformedOnlyRotation(m);
m_y = m_y.TransformedOnlyRotation(m);
}


void CoordinateSystem::GetProperties(std::list<Property *> *list)
{
//list->push_back(PropertyTrsf(this, L"position", &m_p));
list->push_back(new PropertyString(this, L"title", &m_title));
list->push_back(PropertyPnt(this, L"position", &m_o));
list->push_back(PropertyPnt(this, L"x-axis", &m_x));
list->push_back(PropertyPnt(this, L"y-axis", &m_y));

HeeksObj::GetProperties(list);
}

Expand All @@ -1226,7 +1230,7 @@ void CoordinateSystem::GetGripperPositions(std::list<GripData> *list, bool just_

Point3d px(m_o + m_x * s);
Point3d py(m_o + m_y * s);
Point3d vz = Point3d(0, 0, 1).Transformed(mat);
Point3d vz = Point3d(0, 0, 1).Transformed(mat) - m_o;
Point3d pz(m_o + vz * s);
list->push_back(GripData(GripperTypeTranslate,m_o,NULL));
list->push_back(GripData(GripperTypeRotateObject,px,NULL));
Expand Down
4 changes: 2 additions & 2 deletions CAD/DigitizeMode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,9 @@ DigitizedPoint DigitizeMode::Digitize(const Line &ray){
DigitizedPoint point(pnt, DigitizeCoordsType);

if(theApp->draw_to_grid){
Point3d plane_vx = Point3d(1, 0, 0).Transformed(digitizing_matrix());
Point3d plane_vy = Point3d(0, 1, 0).Transformed(digitizing_matrix());
Point3d datum = Point3d(0, 0, 0).Transformed(digitizing_matrix());
Point3d plane_vx = Point3d(1, 0, 0).Transformed(digitizing_matrix()) - datum;
Point3d plane_vy = Point3d(0, 1, 0).Transformed(digitizing_matrix()) - datum;

double a = Point3d(datum) * plane_vx;
double b = Point3d(point.m_point) * plane_vx;
Expand Down
8 changes: 4 additions & 4 deletions CAD/Grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ static void RenderGrid(const CViewPoint *view_point, double max_number_across, b
datum = datum.Transformed(orimat);
orimat = Matrix(datum, vx, vy);
Point3d unit_forward = view_point->forwards_vector().Normalized();
double plane_dp = fabs(Point3d(0, 0, 1).Transformed(orimat) * unit_forward);
double plane_dp = fabs((Point3d(0, 0, 1).Transformed(orimat) - datum) * unit_forward);
if(plane_dp < 0.3)return;
Plane plane(Point3d(0, 0, 0).Transformed(orimat), Point3d(0, 0, 1).Transformed(orimat));
Plane plane(datum, Point3d(0, 0, 1).Transformed(orimat) - datum);
{
for(int i =0; i<4; i++){
Point3d p1 = view_point->glUnproject(sp[i]);
Expand Down Expand Up @@ -169,7 +169,7 @@ void GetGridBox(const CViewPoint *view_point, CBox &ext){
Matrix orimat = *(theApp->GetDrawMatrix(false));
datum = datum.Transformed(orimat);
orimat = Matrix(datum, vx, vy);
Plane plane(datum, Point3d(0, 0, 1).Transformed(orimat));
Plane plane(datum, Point3d(0, 0, 1).Transformed(orimat) - datum);
{
for(int i =0; i<4; i++){
Point3d p1 = view_point->glUnproject(sp[i]);
Expand Down Expand Up @@ -218,7 +218,7 @@ static void RenderGrid(const CViewPoint *view_point, int plane)
Matrix orimat = *(theApp->GetDrawMatrix(false));
datum = datum.Transformed(orimat);
orimat = Matrix(datum, vx, vy);
Point3d v_up = Point3d(0,0, 1).Transformed(orimat);
Point3d v_up = Point3d(0,0, 1).Transformed(orimat) - datum;
double fufz = fabs(uf * v_up);
if(fufz<0.7){
double there = (fufz - 0.3) / 0.4;
Expand Down
27 changes: 27 additions & 0 deletions CAD/PythonStuff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,11 @@ HeeksObj* NewText(const std::wstring &value)
return new HText(Matrix(), value, &theApp->current_color, 0, 0);
}

HeeksObj* NewCoordinateSystem(const std::wstring &title, const Point3d& o, const Point3d& x, const Point3d& y)
{
return new CoordinateSystem(title, o, x, y);
}

boost::python::list HeeksObjGetLines(HeeksObj& object)
{
return_list_ForGetLines = boost::python::list();
Expand Down Expand Up @@ -2004,6 +2009,16 @@ void DeleteObjectsUndoably(boost::python::list &list)
return theApp->DeleteUndoably(o_list);
}

void DeleteInternalObject(HeeksObj* object)
{
delete object;
}

Matrix* GetDrawMatrix(bool get_the_appropriate_orthogonal)
{
return theApp->GetDrawMatrix(get_the_appropriate_orthogonal);
}

void CopyUndoably(HeeksObj* object, HeeksObj* copy_object)
{
theApp->CopyUndoably(object, copy_object);
Expand Down Expand Up @@ -2762,6 +2777,15 @@ BOOST_PYTHON_MODULE(cad) {
.def("NumTriangles", StlSolidNumTriangles)
;

boost::python::class_<CoordinateSystem, boost::python::bases<HeeksObj> >("CoordinateSystem")
.def(boost::python::init<CoordinateSystem>())
.def(boost::python::init<const std::wstring&, const Point3d &, const Point3d &, const Point3d &>()) //, "create with name, origin, x-axis and y-axis"
.def_readwrite("o", &CoordinateSystem::m_o)
.def_readwrite("x", &CoordinateSystem::m_x)
.def_readwrite("y", &CoordinateSystem::m_y)
.def_readwrite("title", &CoordinateSystem::m_title)
;

boost::python::class_<PropertyCheck, boost::noncopyable, boost::python::bases<Property> >("PropertyCheck", boost::python::no_init);
boost::python::class_<PropertyChoice, boost::python::bases<Property> >("PropertyChoice", boost::python::no_init);
boost::python::class_<PropertyColor, boost::python::bases<Property> >("PropertyColor", boost::python::no_init);
Expand Down Expand Up @@ -3217,6 +3241,8 @@ BOOST_PYTHON_MODULE(cad) {
boost::python::def("RollForward", RollForward);
boost::python::def("DeleteUndoably", DeleteUndoably);
boost::python::def("DeleteObjectsUndoably", DeleteObjectsUndoably);
boost::python::def("DeleteInternalObject", DeleteInternalObject);
boost::python::def("GetDrawMatrix", GetDrawMatrix, boost::python::return_value_policy<boost::python::reference_existing_object>());
boost::python::def("AddUndoably", &AddUndoably, AddUndoablyOverloads((boost::python::arg("object"), boost::python::arg("owner") = NULL, boost::python::arg("prev_object") = NULL)));
boost::python::def("CopyUndoably", CopyUndoably);
boost::python::def("TransformUndoably", TransformUndoably);
Expand Down Expand Up @@ -3259,6 +3285,7 @@ BOOST_PYTHON_MODULE(cad) {
boost::python::def("NewCircle", NewCircle, boost::python::return_value_policy<boost::python::reference_existing_object>());
boost::python::def("NewSketch", NewSketch, boost::python::return_value_policy<boost::python::reference_existing_object>());
boost::python::def("NewText", NewText, boost::python::return_value_policy<boost::python::reference_existing_object>());
boost::python::def("NewCoordinateSystem", NewCoordinateSystem, boost::python::return_value_policy<boost::python::reference_existing_object>());
boost::python::def("PyIncref", PyIncref);
boost::python::def("GetNextID", GetNextID);
boost::python::def("GetDrawSelect", GetDrawSelect);
Expand Down
Loading

0 comments on commit 8ff2f04

Please sign in to comment.