-
Notifications
You must be signed in to change notification settings - Fork 2
/
BlockViewDlg.cpp
1131 lines (999 loc) · 38.7 KB
/
BlockViewDlg.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
//
// (C) Copyright 2002-2008 by Autodesk, Inc.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable.
//
//
#include "StdAfx.h"
#if defined(_DEBUG) && !defined(AC_FULL_DEBUG)
//#error _DEBUG should not be defined except in internal Adesk debug builds
#endif
////////////////////////////////////////////////////////////////////////
// include the custom colour palettes
#include "ColorPalette.h"
#include "RgbModel.h"
#include "BlockViewDlg.h"
#include "resource.h"
#include "dbvisualstyle.h"
#include ".\blockviewdlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CBlockViewDlg dialog
extern CAcExtensionModule BlockViewDLL;
CBlockViewDlg::CBlockViewDlg(CWnd *pParent /*=NULL*/)
: CAcUiDialog(CBlockViewDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CBlockViewDlg)
//}}AFX_DATA_INIT
}
void CBlockViewDlg::DoDataExchange(CDataExchange *pDX)
{
CAcUiDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBlockViewDlg)
//}}AFX_DATA_MAP
DDX_Control(pDX, IDC_VIEWMODE, m_viewMode);
}
BEGIN_MESSAGE_MAP(CBlockViewDlg, CAcUiDialog)
//{{AFX_MSG_MAP(CBlockViewDlg)
ON_BN_CLICKED(IDC_ADDENTITY, OnAddentity)
ON_BN_CLICKED(IDC_ADDTEMPENTITY, OnAddtempentity)
ON_BN_CLICKED(IDC_REMAPCOLORS, OnBnClickedRemapcolors)
ON_BN_CLICKED(IDC_STANDARDCOLORS, OnBnClickedStandardcolors)
ON_WM_SIZE()
ON_WM_WINDOWPOSCHANGING()
ON_WM_WINDOWPOSCHANGED()
//}}AFX_MSG_MAP
ON_CBN_SELCHANGE(IDC_VIEWMODE, OnCbnSelchangeViewmode)
ON_COMMAND(ID_TOOLS_PRINT, OnFileOutput)
ON_COMMAND(ID_FILE_ACGSCONFIG, OnFileAcGsConfig)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_COMMAND(ID_SHOWLINETYPES, OnShowlinetypes)
ON_COMMAND(ID_SHOWSECTIONING, OnShowSectioning)
ON_COMMAND(ID_RENDERERTYPE_KDEFAULT, OnRenderertypeKdefault)
ON_COMMAND(ID_RENDERERTYPE_KSOFTWARE, OnRenderertypeKsoftware)
ON_COMMAND(ID_RENDERERTYPE_KSOFTWARENEWVIEWSONLY, OnRenderertypeKsoftwareNewViewsOnly)
ON_COMMAND(ID_RENDERERTYPE_KFULLRENDER, OnRenderertypeKfullRender)
ON_COMMAND(ID_RENDERERTYPE_KSELECTIONRENDER, OnRenderertypeKselectionRender)
ON_COMMAND(ID_ZOOM_WINDOW, OnZoomWindow)
ON_COMMAND(ID_ZOOM_EXTENTS, OnZoomExtents)
ON_COMMAND(ID_SETTINGS_VISUALSTYLE, &CBlockViewDlg::OnSettingsVisualstyle)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBlockViewDlg message handlers
BOOL CBlockViewDlg::OnInitDialog()
{
CAcUiDialog::OnInitDialog();
if (!mPreviewCtrl.SubclassDlgItem(IDC_VIEW,this))
return FALSE;
// now initialise the drawing control with the current working database
InitDrawingControl(acdbHostApplicationServices()->workingDatabase());
// initialise the visual style/view mode combo
InitVisualStyleCombo();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
//***************************************************************************************
// get the view port information - see parameter list
bool GetActiveViewPortInfo (AcDbDatabase *pDb, ads_real &height, ads_real &width,
AcGePoint3d &target, AcGeVector3d &viewDir,
ads_real &viewTwist,
AcDbObjectId ¤tVsId,
bool getViewCenter)
{
// if not ok
if (pDb == NULL)
return false;
// get the current document associated with the PDb database and set it current
AcApDocument *pDoc = acDocManager->document(pDb);
acDocManager->setCurDocument(pDoc);
acDocManager->lockDocument(pDoc);
// make sure the active view port is uptodate
acedVports2VportTableRecords();
// open the view port records
AcDbViewportTablePointer pVTable(pDb->viewportTableId(), AcDb::kForRead);
// if we opened them ok
if (pVTable.openStatus() == Acad::eOk)
{
AcDbViewportTableRecord *pViewPortRec = NULL;
Acad::ErrorStatus es = pVTable->getAt (_T("*Active"), pViewPortRec, AcDb::kForRead);
if (es == Acad::eOk)
{
// get the height of the view
height = pViewPortRec->height ();
// get the width
width = pViewPortRec->width ();
// if the user wants the center of the viewport used
if (getViewCenter == true)
{
struct resbuf rb;
memset (&rb, 0, sizeof (struct resbuf));
// get the system var VIEWCTR
acedGetVar (_T("VIEWCTR"), &rb);
// set that as the target
target = AcGePoint3d (rb.resval.rpoint[X], rb.resval.rpoint[Y], rb.resval.rpoint[Z]);
}
// we want the viewport's camera target setting
else
{
// get the target of the view
target = pViewPortRec->target ();
}
// get the view direction
viewDir = pViewPortRec->viewDirection ();
// get the view twist of the viewport
viewTwist = pViewPortRec->viewTwist ();
// return the current Visual Style
currentVsId = pViewPortRec->visualStyle();
}
// close after use
pViewPortRec->close();
}
acDocManager->unlockDocument(pDoc);
// now restore the original document
acDocManager->setCurDocument(acDocManager->mdiActiveDocument());
return (true);
}
void CBlockViewDlg::OnAddentity()
{
// lock the document
AcAxDocLock lock;
// if ok
if (lock.lockStatus () == Acad::eOk)
{
AcDbObjectPointer<AcDbLine> line;
// call new for the line
line.create();
AcGePoint3d startPoint (rand()/100,rand()/100,rand()/100);
AcGePoint3d endPoint (rand()/100,rand()/100,rand()/100);
line->setStartPoint(startPoint);
line->setEndPoint(endPoint);
// add to the dwg database, to the current space
AcDbBlockTableRecordPointer curSpace(curDoc()->database()->currentSpaceId(), AcDb::kForWrite);
// if ok, add it
if (curSpace.openStatus() == Acad::eOk)
curSpace->appendAcDbEntity(line);
// downgrade to read status
line->downgradeOpen();
curSpace->downgradeOpen();
// add the newly created line to the AcGsView
mPreviewCtrl.mpView->add (line, mPreviewCtrl.model());
// now zoom extents to show where it was drawn
mCurrentDwg->updateExt();
OnZoomExtents();
// update the gsView
refreshView(mPreviewCtrl.mpView);
mPreviewCtrl.SetFocus();
}
}
void CBlockViewDlg::OnAddtempentity()
{
AcGePoint3d startPoint (rand()/100,rand()/100,rand()/100);
AcGePoint3d endPoint (rand()/100,rand()/100,rand()/100);
// create a new line with random coords
AcDbLine *line = new AcDbLine (startPoint, endPoint);
line->setDatabaseDefaults();
// if we created it ok
if (line != NULL)
{
// add the newly created line to the AcGsView
mPreviewCtrl.mpView->add (line, mPreviewCtrl.model());
// next record the temporary entity so we can free it later
mPreviewCtrl.addTempEntity(line);
// now zoom extents to show where it was drawn
//store the temporary lines
m_tempExt.addPoint(startPoint);
m_tempExt.addPoint(endPoint);
if (mCurrentDwg->extmin().distanceTo(mCurrentDwg->extmax()) < 1e20)
{
m_tempExt.addPoint(mCurrentDwg->extmin());
m_tempExt.addPoint(mCurrentDwg->extmax());
}
AcDbExtents viewExtents(m_tempExt);
viewExtents.transformBy(m_viewMatrix);
mPreviewCtrl.mpView->zoomExtents(viewExtents.minPoint(),viewExtents.maxPoint());
// update the gsView
refreshView(mPreviewCtrl.mpView);
mPreviewCtrl.SetFocus();
}
}
void CBlockViewDlg::OnBnClickedRemapcolors()
{
// setup the backgroud colour to the same as Pspace
//
AcColorSettings colourSettings;
// get the AutoCAD current colour settings
acedGetCurrentColors(&colourSettings);
DWORD colorDW;
// get the background colour
colorDW = colourSettings.dwGfxLayoutBkColor;
// get the current device background colour
AcGsColor color = mPreviewCtrl.mpDevice->getBackgroundColor();
// now update them to the new color
color.m_red = GetRValue(colorDW);
color.m_green = GetGValue(colorDW & 0xffff);
color.m_blue = GetBValue(colorDW);
color.m_filler = 255; // opaque background
// finally lets set the new colour
mPreviewCtrl.mpDevice->setBackgroundColor(color);
// now remap the colours, so that on a white background, white lines appear red
mPreviewCtrl.mpDevice->setLogicalPalette (MyAcadColorPs, 256);
// update the gsView
refreshView(mPreviewCtrl.mpView);
mPreviewCtrl.SetFocus();
}
void CBlockViewDlg::OnBnClickedStandardcolors()
{
// setup the backgroud colour to the same as Pspace
//
AcColorSettings colourSettings;
// get the AutoCAD current colour settings
acedGetCurrentColors(&colourSettings);
DWORD colorDW;
// get the background colour
colorDW = colourSettings.dwGfxModelBkColor;
// get the current device background colour
AcGsColor color = mPreviewCtrl.mpDevice->getBackgroundColor();
// now update them to the new color
color.m_red = GetRValue(colorDW);
color.m_green = GetGValue(colorDW & 0xffff);
color.m_blue = GetBValue(colorDW);
color.m_filler = 255; // opaque background
// finally lets set the new colour
mPreviewCtrl.mpDevice->setBackgroundColor(color);
// now remap the colours back to the default
mPreviewCtrl.mpDevice->setLogicalPalette (MyAcadColorMs, 256);
// update the gsView
refreshView(mPreviewCtrl.mpView);
mPreviewCtrl.SetFocus();
}
void CBlockViewDlg::OnSize(UINT nType, int cx, int cy)
{
//CAcUiDialog::OnSize(nType, cx, cy);
CRect rect, clientRect;
CWnd *wnd = NULL;
// now update all the controls
wnd = GetDlgItem (IDC_VIEW);
// if ok
if (wnd != NULL)
{
wnd->MoveWindow(7, 40, cx - 14, cy - 130);
wnd = GetDlgItem (IDC_VIEWMODE);
// get the original coords
wnd->GetWindowRect (rect);
// update the position and size
wnd->MoveWindow(7, 40 + cy - 130 + 7, rect.Width(),rect.Height());
wnd = GetDlgItem (IDC_REMAPCOLORS);
// get the original coords
wnd->GetWindowRect (rect);
// update the position and size
wnd->MoveWindow(150, 40 + cy - 130 + 7, rect.Width(),rect.Height());
wnd = GetDlgItem (IDC_STANDARDCOLORS);
// get the original coords
wnd->GetWindowRect (rect);
// update the position and size
wnd->MoveWindow(250, 40 + cy - 130 + 7, rect.Width(),rect.Height());
// now update the buttons
wnd = GetDlgItem (IDC_ADDENTITY);
// get the original coords
wnd->GetWindowRect (rect);
// update the position and size
wnd->MoveWindow(7, cy - 30, rect.Width(),rect.Height());
wnd = GetDlgItem (IDC_ADDTEMPENTITY);
// get the original coords
wnd->GetWindowRect (rect);
// update the position and size
wnd->MoveWindow(80, cy - 30, rect.Width(),rect.Height());
wnd = GetDlgItem (IDOK);
// get the original coords
wnd->GetWindowRect (rect);
// update the position and size
wnd->MoveWindow(330, cy - 30, rect.Width(),rect.Height());
}
}
//////////////////////////////////////////////////////////////////////////////
// takes a drawing and updates the GsView with it
Acad::ErrorStatus CBlockViewDlg::InitDrawingControl (AcDbDatabase *pDb, const TCHAR *space)
{
// have we got a valid drawing
if (pDb == NULL)
return Acad::eNullBlockName;
// save the passed drawing so that we can use it later in various controls such as zoom extents
mCurrentDwg = pDb;
// initialize values
m_tempExt.minPoint().set(1e20, 1e20, 1e20);
m_tempExt.maxPoint().set(-1e20, -1e20, -1e20);
AcDbBlockTableRecordPointer spaceRec(space, pDb, AcDb::kForRead);
// if ok
if (spaceRec.openStatus() == Acad::eOk)
{
// initialize the preview control
mPreviewCtrl.init(BlockViewDLL.ModuleResourceInstance(),true);
mPreviewCtrl.SetFocus();
AcDbObjectId currentVsId;
// set the view to the *Active* AutoCAD view
currentVsId = SetViewTo(mPreviewCtrl.mpView, pDb, m_viewMatrix);
// tell the view to display this space
mPreviewCtrl.view()->add(spaceRec, mPreviewCtrl.model());
// finally, tell the AcGsView what style to display in
mPreviewCtrl.mpView->setVisualStyle(currentVsId);
}
return spaceRec.openStatus();
}
//////////////////////////////////////////////////////////////////////////
const ACHAR* CBlockViewDlg::getVisualStyleLocalName(AcDbObjectId vsId)
{
const ACHAR *pLocalName = NULL;
// get the owner dictionary entry, to find out the name of the visual style
AcDbDictionaryPointer visualStyleDict(curDoc()->database()->visualStyleDictionaryId(), AcDb::kForRead);
// if ok
if (visualStyleDict.openStatus() == Acad::eOk)
{
ACHAR *pInternalName = NULL;
Acad::ErrorStatus es = visualStyleDict->nameAt(vsId, pInternalName);
if (pInternalName == NULL)
pLocalName = _T("NotSet - Old Drawing");
else
{
if (acdbIsReservedString(pInternalName, AcDb::kVS2DWireframe))
pLocalName = acdbGetReservedString(AcDb::kVS2DWireframe, true);
else if(acdbIsReservedString(pInternalName, AcDb::kVS3DWireframe))
pLocalName = acdbGetReservedString(AcDb::kVS3DWireframe, true);
else if(acdbIsReservedString(pInternalName, AcDb::kVS3DHidden))
pLocalName = acdbGetReservedString(AcDb::kVS3DHidden, true);
else if(acdbIsReservedString(pInternalName, AcDb::kVSRealistic))
pLocalName = acdbGetReservedString(AcDb::kVSRealistic, true);
else if(acdbIsReservedString(pInternalName, AcDb::kVSConceptual))
pLocalName = acdbGetReservedString(AcDb::kVSConceptual, true);
else
pLocalName = pInternalName;
}
}
return pLocalName;
}
//////////////////////////////////////////////////////////////////////////
// Set the current viewport's visual style to the specified visual style
AcDbObjectId CBlockViewDlg::setVisualStyleCurrent(ACHAR *name)
{
AcDbObjectId visualStyleId;
// get the owner dictionary entry, to find out the name of the visual style
AcDbDictionaryPointer visualStyleDict(curDoc()->database()->visualStyleDictionaryId(), AcDb::kForRead);
// if ok
if (visualStyleDict.openStatus() == Acad::eOk)
{
AcDbVisualStyle *pVisualStyleObj;
// Get the visual style
Acad::ErrorStatus es = visualStyleDict->getAt(acdbXlateReservedString(name,false), (AcDbObject *&)pVisualStyleObj, AcDb::kForRead);
// if ok
if (es == Acad::eOk)
{
// Get the visual style's object ID
visualStyleId = pVisualStyleObj->objectId();
pVisualStyleObj->close();
mPreviewCtrl.mpView->setVisualStyle(visualStyleId);
}
}
return visualStyleId;
}
//////////////////////////////////////////////////////////////////////////
void CBlockViewDlg::InitVisualStyleCombo()
{
// get rid of everything
m_viewMode.ResetContent();
// now redraw
AcDbObjectId currentVsId = mPreviewCtrl.mpView->visualStyle();
const ACHAR *pCurrentVsName = getVisualStyleLocalName(currentVsId);
// get all visual styles in the drawing and add them to the view mode/style combo box
AcArray<const ACHAR *> visualStyleList;
if (curDoc()->database()->getVisualStyleList(visualStyleList) == Acad::eOk)
{
for (int i=0; i<visualStyleList.length(); ++i)
{
m_viewMode.AddString(visualStyleList[i]);
// check to see if this is the currently set item
if(!_tcscmp(pCurrentVsName, visualStyleList[i]))
// set the default item
m_viewMode.SetCurSel(i);
}
}
}
//////////////////////////////////////////////////////////////////////////////
void CBlockViewDlg::OnCbnSelchangeViewmode()
{
// find out what we are now set to
TCHAR windowText[MAX_PATH];
m_viewMode.GetWindowTextW(windowText,MAX_PATH);
// and set the view mode
if (setVisualStyleCurrent(windowText) != AcDbObjectId::kNull)
// update the gsView
refreshView(mPreviewCtrl.mpView);
}
//////////////////////////////////////////////////////////////////////////////
// Open a drawing
void CBlockViewDlg::OnFileOpen()
{
struct resbuf *result = NULL;
int status = acedGetFileNavDialog( _T("Select Drawing File"), _T("*.dwg"), _T("DWG"), _T("BlockViewFileOpenDialog"), 0, &result);
if (status == RTNORM)
{
// create a new database to read our drawing into
AcDbDatabase *pDb = new AcDbDatabase (Adesk::kFalse);
// read in the drawing
Acad::ErrorStatus es = pDb->readDwgFile (result->resval.rstring);
// if we didn't read it ok
if (es != Acad::eOk)
{
AfxMessageBox(CString(_T("Sorry, failed to open \"")) + result->resval.rstring + _T("\""));
// then use the current editor drawing
pDb = acdbHostApplicationServices()->workingDatabase();
}
// clear the GsView completely, lets have a fresh start
AcGsView *pView = mPreviewCtrl.view();
pView->eraseAll();
// now initialise the drawing control
InitDrawingControl(pDb);
// refresh view
refreshView(mPreviewCtrl.mpView);
// initialise the visual style/view mode combo
InitVisualStyleCombo();
acutRelRb( result );
}
}
#include <dbrender.h>
//#include <atil.h>
#include "FileWriteDescriptor.h"
#include "RowProviderInterface.h"
#include "FileSpecifier.h"
#include "DataModelAttributes.h"
#include "DataModel.h"
#include "Image.h"
#include <JfifFormatCodec.h>
#include <PngFormatCodec.h>
#include <TiffFormatCodec.h>
#include <BmpFormatCodec.h>
#include <TiffCustomProperties.h>
#include <PngCustomProperties.h>
#include <FileReadDescriptor.h>
// include the libs
// for Atil
#pragma comment (lib, "AdImaging.lib")
#pragma comment (lib, "AdIntImgServices.lib")
// for AcDbMentalRayRenderSettings
#pragma comment (lib, "AcSceneOE.lib") //cph
//////////////////////////////////////////////////////////////////////////////
// output the AcGsView
void CBlockViewDlg::OnFileOutput()
{
// get the filename to output
struct resbuf *result = NULL;
int status = acedGetFileNavDialog(_T("SnapShot"), NULL, _T("jpg;png;tif;bmp"), _T("BlockViewSnapshotBrowseDialog"), 1, &result);
if (status == RTNORM)
{
// extract the size of the Gs View control
CWnd *gsViewWnd = CWnd::GetDlgItem(IDC_VIEW);
CRect sizeRect;
if (gsViewWnd != NULL)
gsViewWnd->GetClientRect(&sizeRect);
else
AfxMessageBox(_T("Failed to get the GsView Control size, the image will be invalid"));
// make the filename var more friendly
ACHAR *pFileName = result->resval.rstring;
// create the image and write to disk
if (!CreateAtilImage(mPreviewCtrl.mpView, sizeRect.Width(), sizeRect.Height(), 32, 0, pFileName))
AfxMessageBox(_T("Failed to create image..."));
}
}
//////////////////////////////////////////////////////////////////////////
// creates an Atil image using AcGsView::getSnapShot()
bool CreateAtilImage(AcGsView *pView,
int width, int height,
int colorDepth, int paletteSize,
ACHAR *pFileName,
bool renderToImage)
{
bool done = false;
AcGsDCRect screenRect(0,width,0, height);
if (!renderToImage)
// get the screen rect from the gsview
pView->getViewport(screenRect);
try
{
// we want colorDepth to be either 24 or 32
if (colorDepth < 24)
colorDepth = 24;
if (colorDepth > 24)
colorDepth = 32;
// create rbgmodel 32 bit true color
Atil::RgbModel rgbModel(colorDepth);
Atil::ImagePixel initialColor(rgbModel.pixelType());
// create the Atil image on the stack
Atil::Image imgSource(Atil::Size(width, height), &rgbModel, initialColor);
// check what output function we require
if (renderToImage)
{
// setup the render settings object
AcDbMentalRayRenderSettings mentalRayRenderer;
// now render to image the gsview
bool ok = pView->renderToImage(&imgSource, &mentalRayRenderer, NULL, screenRect);
// if not ok
if (!ok)
AfxMessageBox(_T("Failed to RenderToImage"));
}
else
{
// get a snapshot of the GsView
pView->getSnapShot(&imgSource, screenRect.m_min);
}
// if ok
if (imgSource.isValid())
{
Atil::RowProviderInterface *pPipe = imgSource.read(imgSource.size(), Atil::Offset(0,0),Atil::kBottomUpLeftRight);
if(pPipe != NULL)
{
TCHAR drive[_MAX_DRIVE];
TCHAR dir[_MAX_DIR];
TCHAR fname[_MAX_FNAME];
TCHAR ext[_MAX_EXT];
// find out what extension we have
_tsplitpath_s(pFileName, drive, dir, fname, ext);
Atil::ImageFormatCodec *pCodec = NULL;
// create the codec depending on what the user chose
if (CString(ext) == _T(".jpg"))
pCodec = new JfifFormatCodec();
else if (CString(ext) == _T(".png"))
pCodec = new PngFormatCodec();
else if (CString(ext) == _T(".tif"))
pCodec = new TiffFormatCodec();
else if (CString(ext) == _T(".bmp"))
pCodec = new BmpFormatCodec();
// if we have a new codec
if (pCodec != NULL)
{
// and it is compatible
if (Atil::FileWriteDescriptor::isCompatibleFormatCodec(pCodec, &(pPipe->dataModel()), pPipe->size()))
{
// create a new file output object
Atil::FileWriteDescriptor fileWriter(pCodec);
Atil::FileSpecifier fs(Atil::StringBuffer((lstrlen(pFileName) + 1) * sizeof(TCHAR),
(const Atil::Byte *)pFileName, Atil::StringBuffer::kUTF_16), Atil::FileSpecifier::kFilePath);
// if the file already exists
// we better delete it because setFileSpecifier will fail otherwise
_tremove(pFileName);
if (fileWriter.setFileSpecifier(fs))
{
fileWriter.createImageFrame(pPipe->dataModel(), pPipe->size());
// At any rate you want to fetch the property from the write file descriptor then alter it and set it in�
Atil::FormatCodecPropertyInterface *pProp = fileWriter.getProperty(Atil::FormatCodecPropertyInterface::kCompression);
if (pProp != NULL)
{
if (CString(ext) == _T(".jpg"))
{
Atil::FormatCodecIntProperty *pComp = dynamic_cast<Atil::FormatCodecIntProperty*>(pProp);
if (pComp != NULL)
{
int min=0, max=0;
pComp->getPropertyLimits(min, max);
// set the quality to 90%
pComp->setValue((int)((double)max * .9));
// apply any changes we have made
fileWriter.setProperty(pComp);
}
}
else if (CString(ext) == _T(".png"))
{
PngCompression *pComp = dynamic_cast<PngCompression*>(pProp);
if (pComp != NULL)
{
// Why not compress all we can?
pComp->selectCompression(PngCompressionType::kHigh);
// apply any changes we have made
fileWriter.setProperty(pComp);
}
}
else if (CString(ext) == _T(".tif"))
{
// All image types can be compressed using kNone, kLZW, and kDeflate.
// If it is 8 bit (palette'd or gray scale) the kPackbits is available.
// If it is 1 bit (bi-tonal) then the CCITT family of kCCITT_* compression of which the kCCITT_FAX4 is the best are available.
// If it is 24 bit (3 channel RGB) then kJPEG is available which will use JPEG DCT compression in the TIFF file.
TiffCompression *pComp = dynamic_cast<TiffCompression*>(pProp);
if (pComp != NULL)
{
// G4 is only valid for 1 bit images.
if (pComp->selectCompression(TiffCompressionType::kCCITT_FAX4) == false)
{
// So if that fails, resort to LZW now that it is patent free
if (pComp->selectCompression(TiffCompressionType::kLZW) == false)
{
// If that fails (and is shouldn�t, be) then set none.
pComp->selectCompression(TiffCompressionType::kNone);
}
}
// apply any changes we have made
fileWriter.setProperty(pComp);
}
}
// clean up
delete pProp;
pProp = NULL;
}
}
// be sure that the format has all the information it needs to write the file.
// In ATIL every property is required to have a valid default value.
// So if a certain property is REQUIRED by the codec to save the file, then it is safe
// and proper to simply loop over all the possible properties, enquire if it is required
// and simply set it with it�s default value
Atil::FormatCodecPropertySetIterator* pPropsIter = fileWriter.newPropertySetIterator();
// if ok
if (pPropsIter)
{
for (pPropsIter->start(); !pPropsIter->endOfList(); pPropsIter->step())
{
Atil::FormatCodecPropertyInterface* pProp = pPropsIter->openProperty();
if (pProp->isRequired())
{
fileWriter.setProperty(pProp);
}
pPropsIter->closeProperty();
}
delete pPropsIter;
}
// ok - ready to write it
fileWriter.writeImageFrame(pPipe);
done = true;
}
}
delete pCodec;
}
}
}
catch ( Atil::ATILException e )
{
const Atil::StringBuffer *pStringMsg =e.getMessage();
}
return done;
}
//////////////////////////////////////////////////////////////////////////////
// display the gs config dialog
void CBlockViewDlg::OnFileAcGsConfig()
{
const TCHAR *regEntry = NULL;
TCHAR regPath[2024];
// Get the current AutoCAD registry path
// e.g. Software\Autodesk\AutoCAD\R17.0\ACAD-301:409
regEntry = acdbHostApplicationServices()->getMachineRegistryProductRootKey();
// this is the path we want
_stprintf_s (regPath, _T("%s\\3DGS Configuration\\GSHEIDI10"), regEntry);
// get the GsManager from current MDI Client Window
AcGsManager *pGsMgr = acgsGetGsManager (NULL);
// if we go the gs manager ok
if (pGsMgr != NULL)
{
// get the graphics kernel
AcGsKernelDescriptor descriptor;
descriptor.addRequirement(AcGsKernelDescriptor::k3DDrawing);
AcGsGraphicsKernel *pGraphicsKernel = AcGsManager::acquireGraphicsKernel(descriptor);
// if ok
if (pGraphicsKernel)
{
// now get the gs config class
AcGsConfig *pConfig = pGraphicsKernel->getConfig();
// if we got this ok too
if (pConfig != NULL)
{
// display the config dialog
//if (pConfig->configure (regPath, true))
if(pConfig->showConfigDialog())
{
// update the gsView
refreshView(mPreviewCtrl.mpView);
}
}
}
}
}
extern Acad::ErrorStatus acgsiSetDeviceLineweightInfo(AcGsDevice* pDevice,
bool bDisplayLineweights, bool bLineweightsAreAnnotation,
double lineweightScale, int lineweightThresholdArea);
void CBlockViewDlg::OnShowlinetypes()
{
// toggle the setting
mPreviewCtrl.mpModel->enableLinetypes(toggleCheckMenuItem(ID_SHOWLINETYPES));
// update the gsView
refreshView(mPreviewCtrl.mpView);
//Undocumented/unsupported API...sets the line weight for AcGsViews...Gopinath found this.
acgsiSetDeviceLineweightInfo(mPreviewCtrl.device(),true,false,3.0f,0);
}
void CBlockViewDlg::OnShowSectioning()
{
// toggle the setting
mPreviewCtrl.mpModel->setEnableSectioning(toggleCheckMenuItem(ID_SHOWSECTIONING));
// update the gsView
refreshView(mPreviewCtrl.mpView);
}
// util function to toggle the tick for a specific menu item
bool CBlockViewDlg::toggleCheckMenuItem(UINT resId)
{
// redirect the MFC resource pointer to us
CAcModuleResourceOverride res;
// find out what the menu it now set to
CMenu *menu = GetMenu();
UINT menuState = menu->GetMenuState(resId, MF_BYCOMMAND);
// now set the menu to the correct checking status
menu->CheckMenuItem(resId, ((menuState & MF_CHECKED) ? MF_UNCHECKED:MF_CHECKED));
// the new setting
return(((menuState & MF_CHECKED) ? false:true));
}
void CBlockViewDlg::OnRenderertypeKdefault()
{
setMenuCheck(ID_RENDERERTYPE_KDEFAULT);
setMenuUnCheck(ID_RENDERERTYPE_KSOFTWARE);
setMenuUnCheck(ID_RENDERERTYPE_KSOFTWARENEWVIEWSONLY);
setMenuUnCheck(ID_RENDERERTYPE_KFULLRENDER);
setMenuUnCheck(ID_RENDERERTYPE_KSELECTIONRENDER);
mPreviewCtrl.mpDevice->setDeviceRenderer(AcGsDevice::kDefault);
// update the gsView
refreshView(mPreviewCtrl.mpView);
}
void CBlockViewDlg::OnRenderertypeKsoftware()
{
setMenuUnCheck(ID_RENDERERTYPE_KDEFAULT);
setMenuCheck(ID_RENDERERTYPE_KSOFTWARE);
setMenuUnCheck(ID_RENDERERTYPE_KSOFTWARENEWVIEWSONLY);
setMenuUnCheck(ID_RENDERERTYPE_KFULLRENDER);
setMenuUnCheck(ID_RENDERERTYPE_KSELECTIONRENDER);
mPreviewCtrl.mpDevice->setDeviceRenderer(AcGsDevice::kSoftware);
// update the gsView
refreshView(mPreviewCtrl.mpView);
}
void CBlockViewDlg::OnRenderertypeKsoftwareNewViewsOnly()
{
setMenuUnCheck(ID_RENDERERTYPE_KDEFAULT);
setMenuUnCheck(ID_RENDERERTYPE_KSOFTWARE);
setMenuCheck(ID_RENDERERTYPE_KSOFTWARENEWVIEWSONLY);
setMenuUnCheck(ID_RENDERERTYPE_KFULLRENDER);
setMenuUnCheck(ID_RENDERERTYPE_KSELECTIONRENDER);
mPreviewCtrl.mpDevice->setDeviceRenderer(AcGsDevice::kSoftwareNewViewsOnly);
// update the gsView
refreshView(mPreviewCtrl.mpView);
}
void CBlockViewDlg::OnRenderertypeKfullRender()
{
setMenuUnCheck(ID_RENDERERTYPE_KDEFAULT);
setMenuUnCheck(ID_RENDERERTYPE_KSOFTWARE);
setMenuUnCheck(ID_RENDERERTYPE_KSOFTWARENEWVIEWSONLY);
setMenuCheck(ID_RENDERERTYPE_KFULLRENDER);
setMenuUnCheck(ID_RENDERERTYPE_KSELECTIONRENDER);
mPreviewCtrl.mpDevice->setDeviceRenderer(AcGsDevice::kFullRender);
// update the gsView
refreshView(mPreviewCtrl.mpView);
}
void CBlockViewDlg::OnRenderertypeKselectionRender()
{
setMenuUnCheck(ID_RENDERERTYPE_KDEFAULT);
setMenuUnCheck(ID_RENDERERTYPE_KSOFTWARE);
setMenuUnCheck(ID_RENDERERTYPE_KSOFTWARENEWVIEWSONLY);
setMenuUnCheck(ID_RENDERERTYPE_KFULLRENDER);
setMenuCheck(ID_RENDERERTYPE_KSELECTIONRENDER);
mPreviewCtrl.mpDevice->setDeviceRenderer(AcGsDevice::kSelectionRender);
// update the gsView
refreshView(mPreviewCtrl.mpView);
}
// util functions for setting the tick for a specific menu item
void CBlockViewDlg::setMenuCheck(UINT resId, UINT nCheck)
{
// redirect the MFC resource pointer to us
CAcModuleResourceOverride res;
// find out what the menu it now set to
CMenu *menu = GetMenu();
// now set the menu to the correct checking status
menu->CheckMenuItem(resId, nCheck);
}
void CBlockViewDlg::setMenuUnCheck(UINT resId)
{
setMenuCheck(resId, MF_UNCHECKED);
}
//////////////////////////////////////////////////////////////////////////
void CBlockViewDlg::OnZoomWindow()
{
// seto to Zoom window
mPreviewCtrl.mZooming = true;
}
void CBlockViewDlg::OnZoomExtents()
{
// count with temp entities as well
AcDbExtents viewExtents(m_tempExt);
if (mCurrentDwg->extmin().distanceTo(mCurrentDwg->extmax()) < 1e20)
{
viewExtents.addPoint(mCurrentDwg->extmin());
viewExtents.addPoint(mCurrentDwg->extmax());
}
viewExtents.transformBy (m_viewMatrix);
mPreviewCtrl.mpView->zoomExtents(viewExtents.minPoint(),viewExtents.maxPoint());
refreshView(mPreviewCtrl.mpView);
}
//////////////////////////////////////////////////////////////////////////
void CBlockViewDlg::OnSettingsVisualstyle()
{
// hide the dialog while we list out what we have
BeginEditorCommand();
// switch to the text screen
acedTextScr();
Acad::ErrorStatus es = Acad::eNotHandled;
// first print out what the visual style is set to
AcDbObjectId currentVsId = mPreviewCtrl.mpView->visualStyle();
// if set
if (currentVsId != AcDbObjectId::kNull)
{
// get the owner dictionary entry, to find out the name of the visual style
AcDbDictionaryPointer visualStyleDict(curDoc()->database()->visualStyleDictionaryId(), AcDb::kForRead);
// if ok
if (visualStyleDict.openStatus() == Acad::eOk)
{
ACHAR *name = NULL;
visualStyleDict->nameAt(currentVsId, name);
acutPrintf(_T("\n\nCurrently set to: %s"), name);
}
}
else
acutPrintf(_T("\n\nCurrently not set.\n"));
// display the visual styles available for selection
acutPrintf(ACRX_T("\n\nVisual Styles available: \n\n"));
int index;
AcArray<const ACHAR *> visualStyleList;
if (curDoc()->database()->getVisualStyleList(visualStyleList) == Acad::eOk)
{
acutPrintf(ACRX_T("0: Sets no Visual Style\n"));
for (index=0; index<visualStyleList.length(); ++index)
acutPrintf(ACRX_T("%d: %s\n"), index+1, visualStyleList[index]);
}
bool quit = false;
do
{
acedInitGet(RSG_NONEG, NULL);
int replyIndex = 1;
// now ask which one he wants
int res = acedGetInt(_T("Select option <1> : "), &replyIndex);
// if ok
if (res != RTCAN)
{
// check the index to make sure it is valid
if (replyIndex > visualStyleList.length())
{
acutPrintf(_T("Please enter a value between 1 and %d..."), visualStyleList.length());
continue;
}
// if reply == 0 then he wants to remove the visual style from the AcGsView
if (replyIndex == 0)
{