-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathap238export.cpp
2409 lines (2083 loc) · 76.1 KB
/
ap238export.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1991-2015 by STEP Tools Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// main routine for AP-238 export
#include "stdafx.h"
#include <comutil.h>
#include <stdio.h>
#include "m_core.h"
#include "m_mastercam.h"
#include "m_mill.h"
#import <mscorlib.tlb>
#import <System.tlb>
#import "C:\\Program Files (x86)\\STEP Tools\\STEP-NC Machine\\stepnc_x64.tlb"
using namespace std;
// COM Smart pointer for the move checker. You can also do it the
// old-school way using CoCreateInstance
stepnc_x64::_AptStepMakerPtr apt;
stepnc_x64::_ProcessPtr process;
stepnc_x64::_FeaturePtr feature;
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
//#define MY_DEBUG 1
#ifdef MY_DEBUG
FILE* pfLog;
#define MY_TRACE fprintf
#else
#define MY_TRACE(...) void(0)
#endif
bool cc1 = true;
bool cc3 = false;
bool use_tp_orientation = true;
bool compound = false; // this flag not fully implemented
bool pattern = false;
double hole_bot;
#define EPSILON 0.01
BOOL VEq(p_3d v1, p_3d v2)
{
return (v1[0] == v2[0] &&
v1[1] == v2[1] &&
v1[2] == v2[2]);
}
// database of chains converted to pockets
// eptr_type pChain_db[1024];
int feature_id_db[1024];
int ws_id_db[1024];
int chain_db_count = 0;
// used to store tool direction and compute CCW vs CW
BOOL tool_direction[1024];
// main functions
void LoadAllTools();
void MakePlanarFace(operation op, p_3d ptCenter, double dLength, double dWidth, _int64 ws_id);
void MakeGeneralOutsideProfile(operation op, CHAIN* pChain, _int64 ws_id);
void MakeGeneralPocket(operation op, CHAIN* pChain, _int64 ws_id, int nChains);
void MakeRoundHole(operation op, p_3d ptCenter, _int64 ws_id);
void MakeRoundPocket(operation op, p_3d ptCenter, double dDiameter, double dDepth, _int64 ws_id);
// helper functions
void MakeChainProfile(
CHAIN* pChain,
double z_value,
BOOL closed,
BOOL reverse_contour_direction
);
void MakeProfileGeometry (
ent seg1, ent seg2,
BOOL first,
BOOL boss_or_open,
double z_value,
BOOL last_in_open,
BOOL reverse_contour_direction
);
BOOL counter_clockwise (p_2d p1, p_2d p2, p_2d center);
void GetChainBBox(
CHAIN* pChain, int nView, double& dLength, double& dWidth, p_3d center
);
// code given to us by Mastercam
// Retrieve the 'tree branches' of items above the Operation
// Used to make nested workplans
void GetOpParents(operation *opl, CStringArray *names);
extern void ap238export()
{
MC_BOOL bResult, bChainResult;
CString strMessage;
nci_bin n;
long ltcode = 0; // lathe tool number
long fpos;
p_3d ptStart, ptEnd, ptBot;
p_3d vecDir, vecRef, vecOldDir = { 0, 0, 1 };
x_matrix mXform1; // wcs change (for origin) apply first
x_matrix mXform2; // tool plane change (for axis) apply second
unsigned nOldOpID = -1;
int nOldGcode = -1;
double nOldRPM = 0;
double dOldFeed = 0.0;
BOOL bAxisChgd = FALSE;
double dRad;
CString strLabel;
BOOL bFirstMove = TRUE;
BOOL bInRapidMode = FALSE;
short nPrevCComp = 0;
p_3d vecSurfNorm;
BOOL bNewOpCheckCComp = FALSE;
chain_manager_info cmi;
int nChains;
short nPock; // decode pocket and island chains
pock_bound* pPB;
CString cmnt; // current comment
long main_q; // question each export or not
double hole_z1, hole_z2, hole_z3; // depth for holes when hole pattern
double hole_speed, hole_feed, hole_plunge, hole_retract;
CString fnme = "export.stpnc";
CFileDialog dlgSave(
FALSE, ".stpnc", fnme, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"STEP-NC Files (*.stpnc)|*.stpnc|STEP Files (*.stp;*.step;*.p21)|*.stp;*.step;*.p21|All Files (*.*)|*.*||", NULL
);
try
{
if (IDOK != dlgSave.DoModal())
{
return;
}
#ifdef MY_DEBUG
pfLog = fopen("C:\\mcamap238exportlog.txt", "w+t");
#endif
// Initialize COM.
HRESULT hr = CoInitialize(NULL);
if (!SUCCEEDED (hr))
{
#ifdef MY_DEBUG
MY_TRACE(pfLog, "COM initialization failed - call STEP Tools \n");
fflush(pfLog);
#endif
MessageBox (0, "COM initialization failed - call STEP Tools", "", MB_OK);
return;
}
hr = apt.CreateInstance(__uuidof(stepnc_x64::AptStepMaker));
if (!SUCCEEDED (hr))
{
#ifdef MY_DEBUG
MY_TRACE(pfLog, "APT initialization failed - call STEP Tools \n");
fflush(pfLog);
#endif
MessageBox (0, "APT initialization failed - call STEP Tools", "", MB_OK);
return;
}
hr = feature.CreateInstance(__uuidof(stepnc_x64::Feature));
if (!SUCCEEDED (hr))
{
#ifdef MY_DEBUG
MY_TRACE(pfLog, "Feature initialization failed - call STEP Tools \n");
fflush(pfLog);
#endif
MessageBox (0, "Feature initialization failed - call STEP Tools", "", MB_OK);
return;
}
hr = process.CreateInstance(__uuidof(stepnc_x64::Process));
if (!SUCCEEDED (hr))
{
#ifdef MY_DEBUG
MY_TRACE(pfLog, "Process initialization failed - call STEP Tools \n");
fflush(pfLog);
#endif
MessageBox (0, "Process initialization failed - call STEP Tools", "", MB_OK);
return;
}
chain_db_count = 0; // database of pocket chains
// get name of the machine group
operation *op0 = TpMainOpMgr.GetMainOpList()[0];
op_group *op_parent = TpMainGrpMgr.GetMainGrpList().GroupByID(op0->cmn.grp_idn);
op_group *op_mach = TpMainGrpMgr.GetMainGrpList().RetrieveRootMachineGroup(op_parent->grp_idn);
CString main_name = op_mach->name;
apt->PartNo("Mastercam Export");
feature->OpenNewWorkpiece ("Mastercam Export");
// if on then use bounded curve geometry
// apt->SetDefineArcUsingViaOff ();
// apt->SetDebuggingNamesOn();
if (IsEnglish())
{
MY_TRACE(pfLog, "Setting units to Inches\n");
apt->Inches();
}
else
{
MY_TRACE(pfLog, "Setting units to Millimeters\n");
apt->Millimeters();
}
apt->CamModeOn ();
apt->MultaxOn();
INT_PTR GrpListSize = TpMainGrpMgr.GetMainGrpList().GetSize ();
CString tmsg;
/* op_group* group0 = TpMainGrpMgr.m_MainGrpList[0];
op_group* group1 = TpMainGrpMgr.m_MainGrpList[1];
tmsg.Format ("List size is %d contains %s and %s", GrpListSize, group0->name, group1->name);
AfxMessageBox(tmsg, MB_YESNO | MB_ICONINFORMATION);*/
op_group* group = NULL;
if (GrpListSize > 0)
{
op_group* group = TpMainGrpMgr.GetMainGrpList()[0];
MY_TRACE(pfLog, "Job setup material = %s at (%f, %f, %f) \n\tlength = %f, width = %f, height = %f\n",
group->ogi.pg2.matl_name,
group->ogi.pg3.cpt[X], group->ogi.pg3.cpt[Y], group->ogi.pg3.cpt[Z],
group->ogi.pg3.x, group->ogi.pg3.y, group->ogi.pg3.z);
if (group->ogi.pg3.x != 0 && group->ogi.pg3.y != 0)
{
process->BlockRawpiece (
"Mastercam block",
group->ogi.pg3.cpt[X],
group->ogi.pg3.cpt[Y],
group->ogi.pg3.cpt[Z] - group->ogi.pg3.z,
group->ogi.pg3.z, group->ogi.pg3.x, group->ogi.pg3.y
);
}
}
LoadAllTools ();
INT_PTR OpListSize = TpMainOpMgr.GetMainOpList().GetSize();
if (OpListSize == 0)
MY_TRACE(pfLog, "Empty Operation list\n");
else
{
operation *opl = NULL;
op_information info;
opl = TpMainOpMgr.GetMainOpList()[0];
op_info (opl->op_idn, TpMainOpMgr.GetMainOpList(), &info);
if (info.is_lathe)
{
MY_TRACE(pfLog, "LATHE\n");
apt->SetModeTurn ();
}
else if (info.is_mill)
{
MY_TRACE(pfLog, "MILL\n");
apt->SetModeMill ();
}
CString message;
CString caption = "STEP-NC Export";
message.Format ("Export all data with no more questions");
main_q = AfxMessageBox(message, MB_YESNO | MB_ICONINFORMATION);
if (main_q == IDYES)
{
MY_TRACE(pfLog, "Exporting all data with no questions asked\n");
}
memset(&mXform1, 0, sizeof(x_matrix));
memset(&mXform2, 0, sizeof(x_matrix));
memset(&ptStart, 0, 3 * sizeof(double));
memset(&vecSurfNorm, 0, 3 * sizeof(double));
// Rewind to the beginning of the NCI file
nci_manager(opl->op_idn, NCIMGR_FSTART, &n, &fpos, &bResult);
// for (INT_PTR OpCount = 3; OpCount < 4; ++OpCount)
CStringArray old_names, new_names;
for (INT_PTR OpCount = 0; OpCount < OpListSize; ++OpCount)
{
MY_TRACE (pfLog, "TOP OF THE LOOP OPCount = %d, OpListSize = %d\n", OpCount, OpListSize);
opl = TpMainOpMgr.GetMainOpList()[OpCount];
char opDescription[80] = "";
op_make_description (opl, FALSE, opDescription, 80);
// workplan hierarchy for the next operation
GetOpParents(opl, &new_names); // Get the "parents" of this operation
if (OpCount == 0)
{
INT_PTR NamesSize = new_names.GetSize ();
MY_TRACE(pfLog, "Mastercam operation stack has height %d\n", NamesSize);
old_names.Add (new_names[0]);
// Edited on July 2 2012 to eliminate "Toolpath Group" nested workplan - was NameSize - 2 now NameSize - 3
// ON Jan 15 2014 changed back to NameSize -2 to get workplan names for Sandvik process data
for (INT_PTR i = NamesSize - 2; i >= 0; i--)
{
MY_TRACE(pfLog, "Making nested workplan called %s\n", new_names[i]);
apt->NestWorkplan (new_names[i].AllocSysString());
}
}
else
{
INT_PTR i, j;
INT_PTR NamesSize = new_names.GetSize ();
INT_PTR OldNamesSize = old_names.GetSize ();
MY_TRACE(pfLog, "Mastercam operation stack: new height %d old height %d\n", NamesSize, OldNamesSize);
// stacks are in reverse order
// Number changed on July 2 2012 to try to be consistent with above - NO TESTING - was -1 now - 2
INT_PTR ii = NamesSize - 2;
INT_PTR jj = OldNamesSize - 2;
// start at end of each and look for first difference
while (ii >= 0 && jj >= 0 && new_names[ii] == old_names[jj])
{
ii--; jj--;
}
// pop off remainder of previous
for (j = 0; j <= jj && j < OldNamesSize - 1; j++)
{
MY_TRACE(pfLog, "Ending workplan called %s\n", old_names[j]);
apt->EndWorkplan ();
}
// ALSO CHANGED DO NOT FEEL GOOD ABOUT THIS ONE
if (ii == NamesSize - 2) i--;
// push new - last is machine group and is ignored
for (i = ii; i >= 0; i--)
{
MY_TRACE(pfLog, "Making nested workplan called %s\n", new_names[i]);
apt->NestWorkplan (new_names[i].AllocSysString());
}
}
old_names.RemoveAll();
INT_PTR NamesSize = new_names.GetSize ();
for (INT_PTR i = 0; i < NamesSize; i++)
{
old_names.Add (new_names[i]);
}
if (main_q == IDNO)
{
CString message;
CString caption = "STEP-NC Export";
message.Format ("Export Operation '%s' (%d of %d)", opDescription, OpCount + 1, OpListSize);
long res = AfxMessageBox(message, MB_YESNO | MB_ICONINFORMATION);
if (res == IDNO)
{
MY_TRACE(pfLog, "Skipping operation %d of %d at user request\n", OpCount + 1, OpListSize);
apt->Workingstep(_com_util::ConvertStringToBSTR (opDescription));
char buf[100];
sprintf (buf, "mc%d.stpnc", OpCount);
apt->ExternalOperation (_com_util::ConvertStringToBSTR (buf));
continue;
}
}
// Create new workingstep (MCAM OP ==> STEP WS)
MY_TRACE(pfLog, "Making new workingstep called %s with comment %s\n", opDescription, opl->comment);
if (strlen (opDescription) > 0)
apt->Workingstep(_com_util::ConvertStringToBSTR (opDescription));
else if (opl->opcode == TP_DRILL)
apt->Workingstep("drill");
else
apt->Workingstep("mill");
// Move to the beginning of the operation stream
nci_manager(opl->op_idn, NCIMGR_RD_SOS, &n, &fpos, &bResult);
// --------------------------------------------------------
// BEGIN reading operations in a loop from NCI data
// --------------------------------------------------------
while (bResult)
{
// Read from the operation stream
nci_manager(opl->op_idn, NCIMGR_RD, &n, &fpos, &bResult);
MY_TRACE(pfLog, "HERE IS THE CURRENT GCODE: %d\n", n.gcode);
if (!bResult || n.op_idn != opl->op_idn)
{
MY_TRACE(pfLog, "Breaking loop\n");
break;
}
cmnt = opl->comment;
if (n.gcode == 20100)
{
ltcode = n.u.l20100.lToolNum;
MY_TRACE(pfLog, "LATHE TOOL SLOT: %d Number %d\n", n.u.l20100.uSlot, ltcode);
}
// dlgProg.m_ctrlProgress.SetPos(opo.slot);
// tp_get_all(n.op_idn, &op, &toolpaths, &ntoolpaths, &bIgnore);
if (n.op_idn != nOldOpID)
{
CString msg;
// msg.Format ("Exporting operation %d (%d:%d)) - %s.", n.op_idn, op.u.op.op_idn, op.u.op.slot, op.u.op.comment);
// AfxMessageBox(msg);
MY_TRACE (pfLog, "Exporting operation %d\n", n.op_idn);
}
/*******************************************************************/
/* Workingstep stuff */
/*******************************************************************/
// Make new workingstep
if (n.op_idn != nOldOpID)
{
int fe_id = 0;
if (ltcode == 0)
{
apt->LoadTool(opl->tl.tlno); // changed from slot to tlno for KTH then back for UTA - KTH almost definitely better
MY_TRACE(pfLog, "Loading tool %d\n", opl->tl.tlno);
bFirstMove = TRUE;
}
else // assume lathe tool
{
apt->LoadTool(ltcode);
MY_TRACE(pfLog, "Lathe Loading tool %d\n", ltcode);
bFirstMove = TRUE;
}
// --------------------------------------------------------
// BEGIN feature extraction
// --------------------------------------------------------
// Now write feature information
MY_TRACE(pfLog, "New op id = %d old op id = %d\n", n.op_idn, nOldOpID);
if (n.op_idn != nOldOpID)
{
// Extract chain
cmi.op_idn = n.op_idn;
cmi.mode = CHNMGR_GET;
cmi.bnd_n_start = 1;
cmi.bnd_n_end = 2;
cmi.chns = NULL;
nChains = 0;
chain_manager(&cmi, -1, &bChainResult);
if (!bChainResult)
{
chain_manager(&cmi, 0, &bChainResult);
nChains = number_of_chains(cmi.chns);
}
if (opl->opcode == TP_POCKET && cc3)
{
// Call after calling "chain_manager" with "CHNMGR_GET"
// This call yields a linked list of "pock_bound",
// which (I believe) parallels the list of chains in "cmi.chns"
// Unfortunately it does not set the "p_b" member in each CHAIN.
// The list contains all pocket bounds, but no island/pocket flags yet.
// chains_to_pock_bounds(cmi.chns, FALSE, FALSE, FALSE, 1.0, &pPB);
// To find out which is apocket and which is an island --
// sort_pock_bounds(NULL, FALSE, FALSE, 1.0, &pPB, &nPock);
MY_TRACE(pfLog, "Number of Pockets %d pb = %x\n", nPock, pPB);
nPock = 1;
if (bChainResult && (nPock == 1 || nPock == 0))
{
MakeGeneralPocket(*opl, cmi.chns, apt->GetCurrentWorkingstep(), nChains);
}
else if (bChainResult)
{
// nFeatureID = MakeCompoundPocket (op.u.op, cmi.chns, apt->GetCurrentWorkingstep(), nPock, pPB);
}
}
else if (opl->opcode == TP_CONTOUR && cc3)
{
if (bChainResult)
{
MakeGeneralOutsideProfile(*opl, cmi.chns, apt->GetCurrentWorkingstep());
}
}
else if (opl->opcode == TP_FACE && cc3)
{
double dLength, dWidth;
p_3d ptCenter;
if (bChainResult) // Chain boundary -- use bounding box
{
GetChainBBox(cmi.chns, opl->cpln.view_n, dLength, dWidth, ptCenter);
}
else if (group != NULL) // Stock boundary (ASSUMED the face is on the top of the stock)
{
MY_TRACE(pfLog, "WARNING: Making planar face using stock dimensions\n");
dLength = group->ogi.pg3.x;
dWidth = group->ogi.pg3.y;
ptCenter[0] = group->ogi.pg3.cpt[X];
ptCenter[1] = group->ogi.pg3.cpt[Y];
ptCenter[2] = group->ogi.pg3.cpt[Z];
}
else
{
dLength = dWidth = ptCenter[0]= ptCenter[1] = ptCenter[2] = 0;
}
MakePlanarFace(*opl, ptCenter, dLength, dWidth, apt->GetCurrentWorkingstep());
if (nChains > 1)
{
// ChainsToBosses(/*nFeatureID,*/ cmi.chns->next); // starting at the second chain in the CMI
}
}
}
}
// --------------------------------------------------------
// END feature extraction
// --------------------------------------------------------
#ifdef MY_DEBUG
fflush(pfLog);
#endif
// should not be doing the below every time
/* if (op.u.op.tl.coolant == 1) {
apt->CoolantOn();
}
else {
apt->CoolantOff();
}
if (tool_direction[op.u.op.tl.slot])
apt->SpindleSpeed(op.u.op.tl.rpm);
else
apt->SpindleSpeed(-op.u.op.tl.rpm);*/
bNewOpCheckCComp = TRUE;
// add operation to instantiate drill cycle
// this used to key off gcode == 0 which lead to issues
if (opl->opcode == TP_DRILL && cc1 && n.gcode == 81)
{
MakeRoundHole(*opl, n.u.m0.ep1, apt->GetCurrentWorkingstep());
// ptStart is probably redundant and currently wrong (not used)
// When switched to code = 81 was found to be at bottom
memcpy(ptStart, n.u.m0.ep1, 3 * sizeof(double));
memcpy(ptEnd, n.u.m0.ep1, 3 * sizeof(double));
memcpy(ptBot, ptEnd, 3 * sizeof(double));
if (opl->cmn.clearance_on)
ptEnd[Z] = ptEnd[Z] - opl->cmn.clearance_pln;
else if (opl->cmn.retract_on) // seems to be same as clearance_plane and not the value called retract in the UI
{
if (opl->cmn.retract_inc)
ptEnd[Z] = ptEnd[Z] - opl->cmn.retract_pln;
else
ptEnd[Z] = ptEnd[Z];
}
memcpy(ptBot, ptEnd, 3 * sizeof(double));
double depth = fabs(opl->cmn.top_stock - opl->cmn.depth);
MY_TRACE(pfLog, "Drilling Depth = %lf, break through = %lf\n", depth, opl->u.prm_drl.brk_thru);
MY_TRACE(pfLog, "z = %lf Clearance = %lf, Retract = %lf feed_pln = %f\n", ptEnd[Z], opl->cmn.clearance_pln,
opl->cmn.retract_pln, opl->cmn.feed_pln);
// depth = depth + op.u.op.u.prm_drl.brk_thru; // KTH magnus says ignore
if (opl->cmn.depth_inc)
ptBot[Z] = ptBot[Z] - depth;
else
ptBot[Z] = opl->cmn.depth;
// As per magnus feed/speed must change at the feed plane point
if (opl->cmn.feed_inc)
ptEnd[Z] = ptEnd[Z] + opl->cmn.feed_pln; // this is the value called retract
else
ptEnd[Z] = opl->cmn.feed_pln; // this is the value called retract
// remember values for other points in pattern
hole_z1 = ptStart[Z];
hole_z2 = ptEnd[Z];
hole_z3 = ptBot[Z];
if (tool_direction[opl->tl.tlno])
hole_speed = opl->tl.rpm;
else
hole_speed = -opl->tl.rpm;
hole_feed = opl->tl.feed;
hole_plunge = opl->tl.plunge;
hole_retract = opl->tl.retract;
// calculate the orientation
if (!use_tp_orientation)
{
xform_pt(ptStart, &mXform1);
xform_pt(ptEnd, &mXform1);
xform_pt(ptBot, &mXform1);
xform_pt(ptStart, &mXform2);
xform_pt(ptEnd, &mXform2);
xform_pt(ptBot, &mXform2);
}
if (nOldRPM != hole_speed)
{
apt->SpindleSpeed (hole_speed); // as per KTH
nOldRPM = hole_speed;
}
MY_TRACE(pfLog, "Retract point at (%lf, %lf, %lf)\n", ptEnd[X], ptEnd[Y], ptEnd[Z]);
// should already be here for first point
// apt->GoToXYZ(cmnt, ptEnd[X], ptEnd[Y], ptEnd[Z]);
apt->Feedrate(hole_feed);
bInRapidMode = FALSE;
apt->GoToXYZ(_com_util::ConvertStringToBSTR (cmnt), ptBot[X], ptBot[Y], ptBot[Z]);
MY_TRACE(pfLog, "Bottom point at (%lf, %lf, %lf)\n", ptBot[X], ptBot[Y], ptBot[Z]);
apt->Feedrate(hole_retract);
apt->GoToXYZ(_com_util::ConvertStringToBSTR (cmnt), ptEnd[X], ptEnd[Y], ptEnd[Z]);
MY_TRACE(pfLog, "Retract point at (%lf, %lf, %lf)\n", ptEnd[X], ptEnd[Y], ptEnd[Z]);
MY_TRACE(pfLog, "Depth = %f, spindle = %f feed = %f, plunge = %f, retract = %f\n",
depth, hole_speed, hole_feed, hole_plunge, hole_retract);
}
else if (opl->opcode == TP_CIRCMILL && cc3 && n.gcode == 0)
{
// this code worked much better here than up above with other features
// consider moving them all to this logic PLEASE
p_3d ptCenter;
double dDiameter = opl->u.circmill.diameter;
memcpy(ptCenter, n.u.m0.ep1, 3 * sizeof(double));
double dDepth;
if (opl->cmn.depth_inc)
{
dDepth = fabs (opl->cmn.depth);
ptCenter[Z] = opl->cmn.top_stock -dDepth;
}
else
{
dDepth = fabs(opl->cmn.top_stock - opl->cmn.depth);
ptCenter[Z] = opl->cmn.depth;
}
MY_TRACE(pfLog, "Circular milling found diameter = %f, depth = %f\n", dDiameter, dDepth);
MY_TRACE(pfLog, "Center = (%f, %f, %f)\n", ptCenter[0], ptCenter[1], ptCenter[2]);
MakeRoundPocket (*opl, ptCenter, dDiameter, dDepth, apt->GetCurrentWorkingstep());
}
// Data now gathered for new operation
if (n.op_idn != nOldOpID && n.gcode < 1000)
nOldOpID = n.op_idn;
// --------------------------------------------------------
// BEGIN toolpath extraction
// --------------------------------------------------------
if (n.gcode == 1027)
{
MY_TRACE(pfLog, "Origin found (%f, %f, %f)\n", n.u.m1027.wcs_origin[0], n.u.m1027.wcs_origin[1], n.u.m1027.wcs_origin[2]);
MY_TRACE(pfLog, "X found (%f, %f, %f)\n", n.u.m1027.wcs_m[0][0], n.u.m1027.wcs_m[0][1], n.u.m1027.wcs_m[0][2]);
MY_TRACE(pfLog, "Y found (%f, %f, %f)\n", n.u.m1027.wcs_m[1][0], n.u.m1027.wcs_m[1][1], n.u.m1027.wcs_m[1][2]);
MY_TRACE(pfLog, "Z found (%f, %f, %f)\n", n.u.m1027.wcs_m[2][0], n.u.m1027.wcs_m[2][1], n.u.m1027.wcs_m[2][2]);
}
// Now act based on the operaton type (G-code)
if (n.gcode == 0 && cc1) // rapid
{
if (nOldRPM != opl->tl.rpm) // as per KTH need to set spindle speed even for rapid
{
if (tool_direction[opl->tl.tlno])
apt->SpindleSpeed(opl->tl.rpm);
else
apt->SpindleSpeed(-opl->tl.rpm);
nOldRPM = opl->tl.rpm;
}
if (n.u.m0.ccomp > 0)
{
bNewOpCheckCComp = FALSE;
if (n.u.m0.ccomp != 0 && cc1)
{
if (n.u.m0.ccomp == 41)
{
MY_TRACE(pfLog, "Left Cutter Contact\n");
apt->Left();
}
else if (n.u.m0.ccomp == 42)
{
MY_TRACE(pfLog, "Right Cutter Contact\n");
apt->Right();
}
else if (n.u.m0.ccomp == COMP_CANCEL)
{
MY_TRACE(pfLog, "Cutter Contact Off\n");
apt->CenterOn();
}
else
MY_TRACE(pfLog, "Cutter Contact type unknown\n");
MY_TRACE(pfLog, "Cutter compensation contact G%d (in control)\n", n.u.m0.ccomp);
}
else if (cc1)
{
apt->CenterOn();
MY_TRACE(pfLog, "Cutter compensation center (in computer)\n");
}
nPrevCComp = n.u.m0.ccomp;
}
if (opl->opcode == TP_POINT) // Assume it's probing (NIST used this type of op)
{
MY_TRACE(pfLog, "Warning TP Point found and ignored\n");
}
else // used to guard this with code to stop if drilling
{
if (!bInRapidMode)
{
apt->MultaxOff();
MY_TRACE(pfLog, "OP %d, Rapid, multax_off, oldcode = %d, newcode = %d\n", n.op_idn, nOldGcode, n.gcode);
apt->Rapid();
bInRapidMode = TRUE;
}
// Geometry
// calculate the orientation of use the toolpath orientation defined in STEP-NC?
if (!use_tp_orientation)
{
xform_pt(n.u.m0.ep1, &mXform1);
xform_pt(n.u.m0.ep1, &mXform2);
}
if (VEq(vecDir, vecOldDir))
{
if (bFirstMove)
{
apt->FirstPathStartPoint (n.u.m0.ep1[0], n.u.m0.ep1[1], n.u.m0.ep1[2]);
bFirstMove = FALSE;
}
// need to generate a G0 code to the first point
apt->GoToXYZ(_com_util::ConvertStringToBSTR (cmnt), n.u.m0.ep1[0], n.u.m0.ep1[1], n.u.m0.ep1[2]);
}
else
{
apt->MultaxOn();
if (bFirstMove)
{
apt->FirstPathStartPoint (n.u.m0.ep1[0], n.u.m0.ep1[1], n.u.m0.ep1[2]);
apt->FirstPathStartAxis (vecDir[0], vecDir[1], vecDir[2]);
bFirstMove = FALSE;
}
apt->GoToXYZ_IJK(_com_util::ConvertStringToBSTR (cmnt), n.u.m0.ep1[0], n.u.m0.ep1[1], n.u.m0.ep1[2],
vecDir[0], vecDir[1], vecDir[2]);
apt->MultaxOff();
// memcpy(vecOldDir, vecDir, 3 * sizeof(double));
}
MY_TRACE(pfLog, "S%d, F%f\n", opl->tl.rpm, n.u.m0.feed);
MY_TRACE(pfLog, "G0 X%f Y%f Z%f I%f J%f K%f\n", n.u.m0.ep1[0], n.u.m0.ep1[1], n.u.m0.ep1[2], vecDir[0], vecDir[1], vecDir[2]);
dOldFeed = fabs(n.u.m0.feed);
}
memcpy(ptStart, n.u.m0.ep1, 3 * sizeof(double));
}
else if (n.gcode == 1 && cc1) // line feed
{
bInRapidMode = FALSE;
if (n.gcode != nOldGcode)
{
MY_TRACE(pfLog, "Old code %d, new code %d multax_off\n", nOldGcode, n.gcode);
apt->MultaxOff();
}
if (nOldRPM != opl->tl.rpm)
{
if (tool_direction[opl->tl.tlno])
apt->SpindleSpeed(opl->tl.rpm);
else
apt->SpindleSpeed(-opl->tl.rpm);
nOldRPM = opl->tl.rpm;
}
if (dOldFeed != n.u.m1.feed)
{
if (opl->tl.use_css)
{
apt->FeedrateCSS (fabs (n.u.m1.feed), opl->tl.max_ss);
MY_TRACE(pfLog, "apt - css feed %f max %d\n", fabs(n.u.m1.feed), opl->tl.max_ss);
}
else
{
apt->Feedrate(fabs (n.u.m1.feed));
MY_TRACE(pfLog, "apt - feed %f\n", fabs(n.u.m1.feed));
}
dOldFeed = n.u.m1.feed;
}
// Cutter compensation (0 => control)
if (n.u.m1.ccomp > 0)
{
bNewOpCheckCComp = FALSE;
if (n.u.m1.ccomp != 0)
{
if (n.u.m1.ccomp == 41)
{
MY_TRACE(pfLog, "Cutter Contact Left\n");
apt->Left();
}
else if (n.u.m1.ccomp == 42)
{
MY_TRACE(pfLog, "Cutter Contact Right\n");
apt->Right();
}
else if (n.u.m1.ccomp == COMP_CANCEL)
{
MY_TRACE(pfLog, "Cutter Contact Off\n");
apt->CenterOn();
}
else
MY_TRACE(pfLog, "Cutter Contact unknown\n");
MY_TRACE(pfLog, "Cutter compensation contact G%d (in control)\n", n.u.m1.ccomp);
}
else
{
apt->CenterOn();
MY_TRACE(pfLog, "Cutter compensation center (in computer)\n");
}
nPrevCComp = n.u.m1.ccomp;
}
// Geometry
// calculate the orientation of use the toolpath orientation defined in STEP-NC?
if (!use_tp_orientation)
{
xform_pt(n.u.m1.ep1, &mXform1);
xform_pt(n.u.m1.ep1, &mXform2);
}
if (VEq(vecDir, vecOldDir))
{
if (bFirstMove)
{
apt->FirstPathStartPoint (n.u.m0.ep1[0], n.u.m0.ep1[1], n.u.m0.ep1[2]);
bFirstMove = FALSE;
}
apt->GoToXYZ(_com_util::ConvertStringToBSTR (cmnt), n.u.m1.ep1[0], n.u.m1.ep1[1], n.u.m1.ep1[2]);
}
else
{
apt->MultaxOn();
if (bFirstMove)
{
apt->FirstPathStartPoint (n.u.m0.ep1[0], n.u.m0.ep1[1], n.u.m0.ep1[2]);
apt->FirstPathStartAxis (vecDir[0], vecDir[1], vecDir[2]);
bFirstMove = FALSE;
}
apt->GoToXYZ_IJK(_com_util::ConvertStringToBSTR (cmnt), n.u.m1.ep1[0], n.u.m1.ep1[1], n.u.m1.ep1[2],
vecDir[0], vecDir[1], vecDir[2]);
apt->MultaxOff();
// memcpy(vecOldDir, vecDir, 3 * sizeof(double));
}
MY_TRACE(pfLog, "Comp = %d\n", n.u.m1.ccomp);
MY_TRACE(pfLog, "S%d, F%f\n", opl->tl.rpm, n.u.m1.feed);
MY_TRACE(pfLog, "G1 X%f Y%f Z%f I%f J%f K%f\n", n.u.m1.ep1[0], n.u.m1.ep1[1], n.u.m1.ep1[2], vecDir[0], vecDir[1], vecDir[2]);
memcpy(ptStart, n.u.m1.ep1, 3 * sizeof(double));
}
else if ((n.gcode == 2 || n.gcode == 3) && cc1) // CW/CCW arc
{
bInRapidMode = FALSE;
if (n.gcode != nOldGcode)
{
MY_TRACE(pfLog, "Old code %d, new code %d multax_off\n", nOldGcode, n.gcode);
apt->MultaxOff();
}
if (nOldRPM != opl->tl.rpm)
{
if (tool_direction[opl->tl.tlno])
apt->SpindleSpeed(opl->tl.rpm);
else
apt->SpindleSpeed(-opl->tl.rpm);
nOldRPM = opl->tl.rpm;
}
if (dOldFeed != n.u.m2.feed)
{
if (opl->tl.use_css)
{
apt->FeedrateCSS (fabs (n.u.m2.feed), opl->tl.max_ss);
MY_TRACE(pfLog, "apt - css feed %f max %d\n", fabs(n.u.m2.feed), opl->tl.max_ss);
}
else
{
apt->Feedrate(fabs (n.u.m2.feed));
MY_TRACE(pfLog, "apt - feed %f\n", fabs(n.u.m2.feed));
}
dOldFeed = n.u.m2.feed;
}
// Cutter compensation (change only if it's new operation or "CANCEL"
if (n.u.m2.ccomp > 0) //bNewOpCheckCComp || (n.u.m2.ccomp == COMP_CANCEL))
{
bNewOpCheckCComp = FALSE;
if (n.u.m2.ccomp != COMP_OFF)
{
if (n.u.m2.ccomp == COMP_LEFT)
{
MY_TRACE(pfLog, "Cutter contact left\n");
apt->Left();
}
else if (n.u.m2.ccomp == COMP_RIGHT)
{
MY_TRACE(pfLog, "Cutter contact right\n");
apt->Right();
}
else if (n.u.m2.ccomp == COMP_CANCEL)
{
MY_TRACE(pfLog, "Cutter contact off\n");
apt->CenterOn();
}
else
MY_TRACE(pfLog, "Cutter Contact unknown\n");
MY_TRACE(pfLog, "Cutter compensation contact G%d (in control)\n", n.u.m2.ccomp);
}
else
{
apt->CenterOn();
MY_TRACE(pfLog, "Cutter compensation center (in computer)\n");
}
nPrevCComp = n.u.m2.ccomp;
}
// Geometry
// calculate the orientation of use the toolpath orientation defined in STEP-NC?
if (!use_tp_orientation)
{
xform_pt(n.u.m2.ep1, &mXform1);
xform_pt(n.u.m2.ep1, &mXform2);
xform_pt(n.u.m2.cpt, &mXform1);
xform_pt(n.u.m2.cpt, &mXform2);
}
dRad = sqrt(pow(n.u.m2.ep1[0] - n.u.m2.cpt[0], 2) +
pow(n.u.m2.ep1[1] - n.u.m2.cpt[1], 2) +
pow(n.u.m2.ep1[2] - n.u.m2.cpt[2], 2));
MY_TRACE(pfLog, "Comp = %d Plane = %d circle = %d position = %d\n", n.u.m2.ccomp, n.u.m2.pln, n.u.m2.circle, n.u.m2.position);
MY_TRACE(pfLog, "S%d, F%f\n", opl->tl.rpm, n.u.m2.feed);
MY_TRACE(pfLog, "%s X%f Y%f Z%f CENTER(X%f Y%f Z%f) R%f\n", n.gcode == 2 ? "G2" : "G3", n.u.m2.ep1[0], n.u.m2.ep1[1], n.u.m2.ep1[2], n.u.m2.cpt[0], n.u.m2.cpt[1], n.u.m2.cpt[2], dRad);
// if (n.u.m2.circle)
// AfxMessageBox("Mastercam FULL CIRCLE found", MB_YESNO | MB_ICONINFORMATION);
if (n.u.m2.pln == 0)
{
int ccw;
// if arc is in plane then simple calculation
if (use_tp_orientation)
{
if (n.gcode == 3)
ccw = 1;