프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / KCOM / Controls / TeighaD3DImage.cs @ f513c215

이력 | 보기 | 이력해설 | 다운로드 (42.3 KB)

1
/////////////////////////////////////////////////////////////////////////////// 
2
// Copyright (C) 2002-2018, Open Design Alliance (the "Alliance"). 
3
// All rights reserved. 
4
// 
5
// This software and its documentation and related materials are owned by 
6
// the Alliance. The software may only be incorporated into application 
7
// programs owned by members of the Alliance, subject to a signed 
8
// Membership Agreement and Supplemental Software License Agreement with the
9
// Alliance. The structure and organization of this software are the valuable  
10
// trade secrets of the Alliance and its suppliers. The software is also 
11
// protected by copyright law and international treaty provisions. Application  
12
// programs incorporating this software must include the following statement 
13
// with their copyright notices:
14
//   
15
//   This application incorporates Teigha(R) software pursuant to a license 
16
//   agreement with Open Design Alliance.
17
//   Teigha(R) Copyright (C) 2002-2018 by Open Design Alliance. 
18
//   All rights reserved.
19
//
20
// By use of this software, its documentation or related materials, you 
21
// acknowledge and accept the above terms.
22
///////////////////////////////////////////////////////////////////////////////
23
///
24
using System;
25
using System.Windows.Interop;
26
using Teigha.Core;
27
using Teigha.TD;
28
using System.Windows;
29
using System.Diagnostics;
30
using System.Windows.Controls;
31
using System.Threading;
32
using Newtonsoft.Json;
33
using System.IO;
34
using System.Reflection;
35
using System.Windows.Input;
36

    
37
namespace KCOM.Controls
38
{
39
    internal class TeighaD3DImage : D3DImage
40
    {
41
        public static Size NewSize = new Size(0, 0);
42
        private static OdDbDatabase TDDatabase = null;
43

    
44
        public delegate void LoadFileDelegate(String fPath);
45
        public delegate void RunTestDelegate();
46

    
47
        public LoadFileDelegate loadFile = null;
48

    
49
        private OdRxObject odRxObjectSurface = null;
50
        private OdRxVariantValue surfaceVariantValue = null;
51

    
52
        class SystemServices : RxSystemServicesImpl
53
        {
54
            public SystemServices()
55
            {
56
                Teigha.Core.Globals.odActivate(ActivationData.userInfo, ActivationData.userSignature);
57
            }
58
        }
59

    
60
        public String FilePath
61
        {
62
            get { return pFilePath; }
63
            set
64
            {
65
                pFilePath = value;
66
                if (null != loadFile)
67
                {
68
                    loadFile(pFilePath);
69
                }
70
            }
71
        }
72

    
73
        public OdGeExtents3d extents { get; private set; }
74

    
75
        public String pFilePath = String.Empty;
76
        private SystemServices Serv = null;
77
        public ExHostAppServices HostApp = null;
78
        uint[] curPalette;
79
        public static OdGsView pView = null;
80

    
81
        OdGsModule pGs = null;
82
        public static OdGsDevice mDevice = null;
83
        public static OdGsLayoutHelper mHelper = null;
84

    
85
        public OdGePoint3d[] PointLoad()
86
        {
87
            OdGePoint3d[] pt = new OdGePoint3d[4];
88
            OdGePoint3d extMax = TDDatabase.getEXTMAX();
89
            OdGePoint3d extMin = TDDatabase.getEXTMIN();
90

    
91
            pt[0] = extMin; //left, bottom
92
            pt[1] = new OdGePoint3d(0.0, extMax.y, 0.0); //left, top
93
            pt[2] = extMax; //right, top
94
            pt[3] = new OdGePoint3d(extMax.x, 0.0, 0.0); //right, bottom            
95

    
96
            if (pView != null)
97
            {
98
                Lock();
99
                using (startPolyLine = OdDbPolyline.createObject())
100
                {
101
                    startPolyLine.addVertexAt(0, new OdGePoint2d(pt[1].x - 4, pt[1].y - 4));
102
                    startPolyLine.addVertexAt(1, new OdGePoint2d(pt[1].x - 4, pt[1].y + 4));
103
                    startPolyLine.addVertexAt(2, new OdGePoint2d(pt[1].x + 4, pt[1].y + 4));
104
                    startPolyLine.addVertexAt(3, new OdGePoint2d(pt[1].x + 4, pt[1].y - 4));
105
                    startPolyLine.setClosed(true);
106
                    startPolyLine.setLineWeight(LineWeight.kLnWt211);
107
                    startPolyLine.setColorIndex((ushort)(OdCmEntityColor.ACIcolorMethod.kACIGreen));
108

    
109
                    OdDbBlockTableRecord model = (OdDbBlockTableRecord)TDDatabase.getModelSpaceId().safeOpenObject(OpenMode.kForWrite);
110
                    startOdDbObjectId = model.appendOdDbEntity(startPolyLine);
111
                }
112

    
113
                using (endPolyLine = OdDbPolyline.createObject())
114
                {
115
                    endPolyLine.addVertexAt(0, new OdGePoint2d(pt[3].x - 4, pt[3].y - 4));
116
                    endPolyLine.addVertexAt(1, new OdGePoint2d(pt[3].x - 4, pt[3].y + 4));
117
                    endPolyLine.addVertexAt(2, new OdGePoint2d(pt[3].x + 4, pt[3].y + 4));
118
                    endPolyLine.addVertexAt(3, new OdGePoint2d(pt[3].x + 4, pt[3].y - 4));
119
                    endPolyLine.setClosed(true);
120
                    endPolyLine.setLineWeight(LineWeight.kLnWt211);
121
                    endPolyLine.setColorIndex((ushort)(OdCmEntityColor.ACIcolorMethod.kACIGreen));
122
                    OdDbBlockTableRecord model = (OdDbBlockTableRecord)TDDatabase.getModelSpaceId().safeOpenObject(OpenMode.kForWrite);
123
                    endOdDbObjectId = model.appendOdDbEntity(endPolyLine);
124
                }
125

    
126
                mDevice.invalidate();
127
                mDevice.update(new OdGsDCRect(0, (int)NewSize.Width, (int)NewSize.Height, 0));//mDevice.update();
128
                UpdateDirtyRect();
129
                Unlock();
130
            }
131
            return pt;
132
        }
133

    
134
        public HwndSource wndSource = null;
135

    
136
        public TeighaD3DImage()
137
        {
138
            Serv = new SystemServices();
139
            HostApp = new CustomServices();
140
            TD_Db.odInitialize(Serv);
141
            Globals.odgsInitialize();
142
            InitializeGDIModule();
143
            loadFile = new LoadFileDelegate(LoadDWGFile);
144

    
145
            if (String.Empty != pFilePath)
146
            {
147
                loadFile(pFilePath);
148
                //InitializeSurface();
149
            }
150
        }
151
        private static OdGePoint3d getWorldCoordinateByScreen(OdGsLayoutHelper hDevice, OdGePoint2d screenPoint)
152
        {
153
            pView = hDevice.activeView();
154

    
155
            //directions, origin, etc
156
            OdGePoint3d target = pView.target();
157
            OdGeVector3d direction = target - pView.position();
158
            OdGeVector3d yVector = pView.upVector();
159
            OdGeVector3d xVector = direction.crossProduct(yVector).normal();
160

    
161
            OdGePoint3d center, stepX, stepY;
162
            {
163
                center = new OdGePoint3d(target);
164
                stepX = target + xVector;
165
                stepY = target + yVector;
166

    
167
                OdGeMatrix3d matrix = pView.worldToDeviceMatrix();
168
                stepX.transformBy(matrix);
169
                stepY.transformBy(matrix);
170
                center.transformBy(matrix);
171
            }
172

    
173
            //make the calculation
174
            OdGePoint3d result;
175
            {
176
                double x, y;
177
                x = (screenPoint.x - center.x) / (stepX.x - center.x);
178
                y = (screenPoint.y - center.y) / (stepY.y - center.y);
179

    
180
                result = target + xVector * x + yVector * y;
181
            }
182

    
183
            return result;
184
        }
185

    
186

    
187
        public void OnMouseWheel(MouseWheelEventArgs e, Point cp)
188
        {
189
            if (mDevice == null)
190
                return;
191
            OdGePoint2d correctScreenPoint = new OdGePoint2d(cp.X, cp.Y);
192

    
193
            OdGePoint3d worldPoint = getWorldCoordinateByScreen(mHelper, correctScreenPoint);
194

    
195
            //OnZoom(e.Delta / 120);
196
            Zoom(e.Delta > 0);
197
            //shift back
198
            pView = mHelper.activeView();
199

    
200
            //get new screen point of the same world point
201
            OdGeMatrix3d worldToDevice = pView.worldToDeviceMatrix();
202
            OdGePoint3d newScreenPoint = worldToDevice * worldPoint;
203

    
204
            //get world points on the focal plane
205
            OdGePoint3d movingPoint = getWorldCoordinateByScreen(mHelper, new OdGePoint2d(newScreenPoint.x, newScreenPoint.y));
206
            OdGePoint3d destinationPoint = getWorldCoordinateByScreen(mHelper, correctScreenPoint);
207

    
208
            OdGePoint3d position = pView.position(), target = pView.target();
209
            OdGeVector3d upVector = pView.upVector();
210
            double width = pView.fieldWidth(), height = pView.fieldHeight();
211
            bool isPerspective = pView.isPerspective();
212

    
213
            //shift the camera so points coincide
214
            OdGeVector3d offset = destinationPoint - movingPoint;
215
            position -= offset;
216
            target -= offset;
217

    
218
            pView.setView(position, target, upVector, width, height, isPerspective ? OdGsView.Projection.kPerspective : OdGsView.Projection.kParallel);
219

    
220
        }
221

    
222
        public void LoadDWGFile(String filePath)
223
        {
224
            TDDatabase = HostApp.readFile(filePath);
225
            if (null == TDDatabase)
226
            {
227
                return;
228
            }
229
            // Create a new OdGsDevice object, and associate with the vectorization GsDevice
230
            mDevice = pGs.createDevice();
231
            // use the wndSource to set the WindowHWND parameter of device - we put the handle of auxiliary invisible window
232
            mDevice.properties().putAt("WindowHWND", new OdRxVariantValue(wndSource.Handle.ToInt32()));
233
            // Set the device background color and palette
234
            curPalette = AllPalettes.getLightPalette();
235
            mDevice.setBackgroundColor(curPalette[0]);
236
            mDevice.setLogicalPalette(curPalette, 256);
237

    
238
            OdGiContextForDbDatabase ctx1 = OdGiContextForDbDatabase.createObject();
239
            ctx1.enableGsModel(true);
240
            ctx1.setDatabase(TDDatabase);
241
            mDevice.setUserGiContext(ctx1);
242
            //   mDevice = (OdGsDevice)TD_Db.setupActiveLayoutViews(mDevice, ctx1);
243
            mHelper = CreateLayoutHelper(mDevice, ctx1);
244

    
245

    
246
            bool bModelSpace = TDDatabase.getTILEMODE();
247

    
248
            pView = mDevice.viewAt(0);
249
            OdAbstractViewPE pViewPe = OdAbstractViewPE.cast(pView);
250
            pViewPe.zoomExtents(pView);
251
            //  OdDbObject pView = TDDatabase.activeViewportId().openObject();
252
            //  OdDbAbstractViewportData pViewPE = OdDbAbstractViewportData.cast(pView);
253
            //   pViewPE.zoomExtents(pView);
254

    
255
            if ((NewSize.Width != 0) && (NewSize.Height != 0))
256
            {
257
                mDevice.onSize(new OdGsDCRect(0, (int)NewSize.Width, (int)NewSize.Height, 0));
258
            }
259
            else
260
            {
261
                mDevice.onSize(new OdGsDCRect(0, 300, 300, 0));
262
            }
263
            InitializeSurface();
264
            // call OnRnderSizeChanged to adjust drawing to size of control, not to size of invisible window
265
            OnRenderSizeChanged(NewSize);
266
        }
267

    
268
        public static OdGiContextForDbDatabase CreateContextForDbDatabase(OdDbDatabase db)
269
        {
270
            OdGiContextForDbDatabase ctx = OdGiContextForDbDatabase.createObject();
271

    
272
            ctx.enableGsModel(true);
273

    
274
            ctx.setDatabase(db);
275

    
276
            return ctx;
277
        }
278
        public static OdGsLayoutHelper CreateLayoutHelper(OdGsDevice pDevice, OdGiContextForDbDatabase ctx)
279
        {
280
            OdGsLayoutHelper hDevice = TD_Db.setupActiveLayoutViews(pDevice, ctx);
281
            OdDbDatabase db = ctx.getDatabase();
282

    
283
            // Set the device background color and palette
284
            uint[] CurPalette = Teigha.Core.AllPalettes.getLightPalette();
285
            hDevice.setBackgroundColor(CurPalette[0]);
286
            hDevice.setLogicalPalette(CurPalette, 256);
287

    
288
            // Return true if and only the current layout is a paper space layout
289
            bool bModelSpace = db.getTILEMODE();
290

    
291
            // Set the viewport border properties
292
            SetViewportBorderProperties(hDevice, !bModelSpace, CurPalette[7]);
293

    
294
            //Set render mode
295
            hDevice.activeView().setMode(OdGsView.RenderMode.kFlatShadedWithWireframe);
296

    
297
            return hDevice;
298
        }
299

    
300
        private static void SetViewportBorderProperties(OdGsLayoutHelper pDevice, bool bModel, uint color7)
301
        {
302
            int n = pDevice.numViews();
303

    
304
            if (n > 1)
305
            {
306
                for (int i = bModel ? 0 : 1; i < n; ++i)
307
                {
308
                    // Get the viewport
309
                    pView = pDevice.viewAt(i);
310

    
311
                    // Make it visible
312
                    pView.setViewportBorderVisibility(true);
313

    
314
                    // Set the color and width
315
                    pView.setViewportBorderProperties(color7, 1);
316
                }
317
            }
318
        }
319

    
320
        private static OdGsDCPoint[] SelectionPointsByPointOnScreen(OdGsLayoutHelper dev, OdDbDatabase db, int x, int y)
321
        {
322
            OdGePoint3d pt = ScreenPointToWorld(dev, x, y);
323
            pt = pt.transformBy(dev.activeView().worldToDeviceMatrix());
324
            short pb = db.getSysVar("PICKBOX").getInt16();
325
            OdGsDCPoint pt1 = new OdGsDCPoint((int)pt.x - pb, (int)pt.y - pb);
326
            OdGsDCPoint pt2 = new OdGsDCPoint((int)pt.x + pb, (int)pt.y + pb);
327
            OdGsDCPoint[] pts = new OdGsDCPoint[] { pt1, pt2 };
328
            return pts;
329
        }
330

    
331
        public static OdGePoint3d ScreenPointToWorld(OdGsLayoutHelper helperDevice, double x, double y)
332
        {
333
            MemoryTransaction tr = MemoryManager.GetMemoryManager().StartTransaction();
334
            OdGsView pView = helperDevice.activeView();
335
            OdGePoint3d wcsPt = new OdGePoint3d(x, y, 0.0);
336
            wcsPt = wcsPt.transformBy((pView.screenMatrix() * pView.projectionMatrix()).inverse());
337
            wcsPt = new OdGePoint3d(wcsPt.x, wcsPt.y, 0.0);
338
            OdAbstractViewPE pVpPE = OdAbstractViewPE.cast(pView);
339
            OdGePoint3d result = wcsPt.transformBy(pVpPE.eyeToWorld(pView));
340
            MemoryManager.GetMemoryManager().StopTransaction(tr);
341
            return result;
342
        }
343

    
344
        private void InitializeGDIModule()
345
        {
346
            try
347
            {
348
                pGs = (OdGsModule)Globals.odrxDynamicLinker().loadModule("WinDirectX.txv", false);
349
            }
350
            catch (Exception err)
351
            {
352
                MessageBox.Show(err.Message, "Error1");
353
                return;
354
            }
355
        }
356

    
357
        public void OnRenderSizeChanged(Size size)
358
        {
359
            NewSize = size;
360
            if (null != mDevice)
361
            {                
362
                {
363
                    Lock();
364

    
365
                    SetBackBuffer(D3DResourceType.IDirect3DSurface9, IntPtr.Zero);
366
                    Unlock();
367
                    OdRxObject.ReleasePointer(odRxObjectSurface);
368
                    mDevice.onSize(new OdGsDCRect(0, (int)NewSize.Width, (int)NewSize.Height, 0));
369
                    mDevice.update(new OdGsDCRect(0, (int)NewSize.Width, (int)NewSize.Height, 0));//mDevice.update();
370
                    odRxObjectSurface = mDevice.properties().getAt("D3DSurface");
371
                    surfaceVariantValue = new OdRxVariantValue(odRxObjectSurface);
372
                    IntPtr surface = IntPtr.Zero;
373
                    if (IntPtr.Size == 8)
374
                    {
375
                        surface = (IntPtr)surfaceVariantValue.AsInt64();
376
                    }
377
                    else
378
                    {
379
                        surface = (IntPtr)surfaceVariantValue.AsInt32();
380
                    }
381

    
382
                    if (IntPtr.Zero != surface)
383
                    {
384
                        Lock();
385
                        SetBackBuffer(D3DResourceType.IDirect3DSurface9, surface);
386
                        UpdateDirtyRect();
387
                        Unlock();
388
                    }
389
                }
390
            }
391
        }
392

    
393
        public void InitializeSurface()
394
        {
395
            if (IsFrontBufferAvailable)
396
            {
397
                if (null != mDevice)
398
                {
399
                    mDevice.update(new OdGsDCRect(0, (int)NewSize.Width, (int)NewSize.Height, 0));//mDevice.update();
400
                    if (mDevice.properties().has("D3DSurface"))
401
                    {
402
                        odRxObjectSurface = mDevice.properties().getAt("D3DSurface");
403
                        surfaceVariantValue = new OdRxVariantValue(odRxObjectSurface);
404
                        IntPtr surface = IntPtr.Zero;
405
                        if (IntPtr.Size == 8)
406
                        {
407
                            surface = (IntPtr)surfaceVariantValue.AsInt64();
408
                        }
409
                        else
410
                        {
411
                            surface = (IntPtr)surfaceVariantValue.AsInt32();
412
                        }
413
                        if (IntPtr.Zero != surface)
414
                        {
415
                            Lock();
416
                            SetBackBuffer(D3DResourceType.IDirect3DSurface9, surface);
417
                            UpdateDirtyRect();
418
                            Unlock();
419
                        }
420
                    }
421
                }
422
            }
423
        }
424

    
425
        void UpdateDirtyRect()
426
        {
427
            Int32Rect updateRect = new Int32Rect();
428
            updateRect.X = updateRect.Y = 0;
429
            updateRect.Width = this.PixelWidth;
430
            updateRect.Height = this.PixelHeight;
431
            this.AddDirtyRect(updateRect);
432
        }
433

    
434
        public void Zoom(bool IsZoomIn)
435
        {
436
            if (pView != null)
437
            {
438
                Lock();
439
                pView.zoom(IsZoomIn ? 1.0 / 0.9 : 0.9);
440
                mDevice.update(new OdGsDCRect(0, (int)NewSize.Width, (int)NewSize.Height, 0));//mDevice.update();
441
                UpdateDirtyRect();
442
                Unlock();
443
            }
444
        }
445

    
446
        public void Dolly(double x, double y, double z)
447
        {
448
            if (pView != null)
449
            {
450
                Lock();
451
                pView.dolly(x, y, z);
452
                mDevice.update(new OdGsDCRect(0, (int)NewSize.Width, (int)NewSize.Height, 0));//mDevice.update();
453
                UpdateDirtyRect();
454
                Unlock();
455
            }
456
        }
457

    
458
        public void OnMouseDolly(MouseEventArgs e, Point sp, Point cp)
459
        {
460
            if (mDevice == null)
461
                return;
462
            OdGePoint2d sScreenPoint = new OdGePoint2d(sp.X, sp.Y);
463
            OdGePoint2d cScreenPoint = new OdGePoint2d(cp.X, cp.Y);
464
            OdGePoint3d sPoint = getWorldCoordinateByScreen(mHelper, sScreenPoint);
465
            OdGePoint3d cPoint = getWorldCoordinateByScreen(mHelper, cScreenPoint);
466
            OdGePoint3d worldPoint = new OdGePoint3d(sPoint.x - cPoint.x, sPoint.y - cPoint.y, 0);
467
            Dolly(worldPoint.x, worldPoint.y, worldPoint.z);
468
        }
469

    
470
        private OdDbPolyline startPolyLine;
471
        private OdDbPolyline endPolyLine;
472
        private static OdDbObjectId startOdDbObjectId;
473
        private static OdDbObjectId endOdDbObjectId;
474

    
475
        public void DrawPoint(OdGePoint3d point, DrawType drawType)
476
        {
477
            if (pView != null)
478
            {
479
                Lock();
480

    
481
                if (drawType == DrawType.sp)
482
                {
483
                    //지우고
484
                    if (startOdDbObjectId != null)
485
                    {
486
                        OdDbObject obj = startOdDbObjectId.safeOpenObject(OpenMode.kForWrite);
487
                        obj.erase();
488
                        startOdDbObjectId = null;
489
                    }
490

    
491
                    //그리고
492
                    using (OdDbPolyline newStartPolyLine = OdDbPolyline.createObject())
493
                    {
494
                        newStartPolyLine.addVertexAt(0, new OdGePoint2d(point.x - 4, point.y - 4));
495
                        newStartPolyLine.addVertexAt(1, new OdGePoint2d(point.x - 4, point.y + 4));
496
                        newStartPolyLine.addVertexAt(2, new OdGePoint2d(point.x + 4, point.y + 4));
497
                        newStartPolyLine.addVertexAt(3, new OdGePoint2d(point.x + 4, point.y - 4));
498
                        newStartPolyLine.setClosed(true);
499
                        newStartPolyLine.setLineWeight(LineWeight.kLnWt211);
500
                        newStartPolyLine.setColorIndex((ushort)(OdCmEntityColor.ACIcolorMethod.kACIGreen));
501
                        OdDbBlockTableRecord model = (OdDbBlockTableRecord)TDDatabase.getModelSpaceId().safeOpenObject(OpenMode.kForWrite);
502
                        startOdDbObjectId = model.appendOdDbEntity(newStartPolyLine);
503
                    }
504
                }
505
                else if (drawType == DrawType.ep)
506
                {
507
                    //지우고
508
                    if (endOdDbObjectId != null)
509
                    {
510
                        OdDbObject obj = endOdDbObjectId.safeOpenObject(OpenMode.kForWrite);
511
                        obj.erase();
512
                        endOdDbObjectId = null;
513
                    }
514
                    //그리고
515
                    using (OdDbPolyline newEndPolyLine = OdDbPolyline.createObject())
516
                    {
517
                        newEndPolyLine.addVertexAt(0, new OdGePoint2d(point.x - 4, point.y - 4));
518
                        newEndPolyLine.addVertexAt(1, new OdGePoint2d(point.x - 4, point.y + 4));
519
                        newEndPolyLine.addVertexAt(2, new OdGePoint2d(point.x + 4, point.y + 4));
520
                        newEndPolyLine.addVertexAt(3, new OdGePoint2d(point.x + 4, point.y - 4));
521
                        newEndPolyLine.setClosed(true);
522
                        newEndPolyLine.setLineWeight(LineWeight.kLnWt211);
523
                        newEndPolyLine.setColorIndex((ushort)(OdCmEntityColor.ACIcolorMethod.kACIGreen));
524
                        OdDbBlockTableRecord model = (OdDbBlockTableRecord)TDDatabase.getModelSpaceId().safeOpenObject(OpenMode.kForWrite);
525
                        endOdDbObjectId = model.appendOdDbEntity(newEndPolyLine);
526
                    }
527
                }
528

    
529
                mDevice.invalidate();
530
                mDevice.update(new OdGsDCRect(0, (int)NewSize.Width, (int)NewSize.Height, 0));//mDevice.update();
531
                UpdateDirtyRect();
532
                Unlock();
533
            }
534
        }
535

    
536
        public void DeleteContext()
537
        {
538
            if (mDevice != null || mHelper != null)
539
            {
540
                mDevice.Dispose();
541
                mHelper.Dispose();
542
                mDevice = null;
543
                mHelper = null;
544
                //TDDatabase = null;
545
                //loadFile = null;
546
                if (odRxObjectSurface != null)
547
                {
548
                    odRxObjectSurface.Dispose();
549
                    odRxObjectSurface = null;
550
                }
551

    
552
                if (TDDatabase != null)
553
                {
554
                    TDDatabase = null;
555
                }
556

    
557
                if (loadFile != null)
558
                {
559
                    loadFile = null;
560
                }
561
            }
562
        }
563

    
564
        String toString(OdDbObjectId val)
565
        {
566
            if (val.isNull())
567
            {
568
                return "Null";
569
            }
570

    
571
            if (val.isErased())
572
            {
573
                return "Erased";
574
            }
575

    
576
            /**********************************************************************/
577
            /* Open the object                                                    */
578
            /**********************************************************************/
579
            OdDbObject pObject = val.safeOpenObject();
580

    
581
            /**********************************************************************/
582
            /* Return the name of an OdDbSymbolTableRecord                        */
583
            /**********************************************************************/
584
            if (pObject is OdDbSymbolTableRecord)
585
            {
586
                OdDbSymbolTableRecord pSTR = (OdDbSymbolTableRecord)pObject;
587
                return pSTR.getName();
588
            }
589

    
590
            /**********************************************************************/
591
            /* Return the name of an OdDbMlineStyle                               */
592
            /**********************************************************************/
593
            if (pObject is OdDbMlineStyle)
594
            {
595
                OdDbMlineStyle pStyle = (OdDbMlineStyle)pObject;
596
                return pStyle.name();
597
            }
598

    
599
            /**********************************************************************/
600
            /* Return the name of a PlotStyle                                      */
601
            /**********************************************************************/
602
            if (pObject is OdDbPlaceHolder)
603
            {
604
                OdDbDictionary pDictionary = (OdDbDictionary)val.database().getPlotStyleNameDictionaryId().safeOpenObject();
605
                String plotStyleName = pDictionary.nameAt(val);
606
                return plotStyleName;
607
            }
608

    
609
            /**********************************************************************/
610
            /* Return the name of an OdDbMaterial                                 */
611
            /**********************************************************************/
612
            if (pObject is OdDbMaterial)
613
            {
614
                OdDbMaterial pMaterial = (OdDbMaterial)pObject;
615
                return pMaterial.name();
616
            }
617

    
618
            /**********************************************************************/
619
            /* We don't know what it is, so return the description of the object  */
620
            /* object specified by the ObjectId                                   */
621
            /**********************************************************************/
622
            return pObject.isA().name();
623
        }
624
        public void DrawLine()
625
        {
626
            if (pView != null)
627
            {
628
                Lock();
629
                using (OdDbLine l = OdDbLine.createObject())
630
                {
631
                    l.setStartPoint(new OdGePoint3d(0, 0, 0));
632
                    l.setEndPoint(new OdGePoint3d(1000, 1000, 0));
633
                    l.setLineWeight(LineWeight.kLnWt200);
634

    
635
                    OdDbBlockTableRecord model = (OdDbBlockTableRecord)TDDatabase.getModelSpaceId().safeOpenObject(OpenMode.kForWrite);
636

    
637
                    model.appendOdDbEntity(l);
638
                }
639
                mDevice.invalidate();
640
                mDevice.update(new OdGsDCRect(0, (int)NewSize.Width, (int)NewSize.Height, 0));//mDevice.update();
641
                UpdateDirtyRect();
642
                Unlock();
643
            }
644
        }
645

    
646
        static OdDbPolygonMesh_Dumper m_polygonMesh = new OdDbPolygonMesh_Dumper();
647
        class OdDbPolygonMesh_Dumper : OdDbPropertiesOverrule
648
        {
649
            public override bool isApplicable(OdRxObject pOverruledSubject)
650
            {
651
                return true;
652
            }
653
            public override void list(OdDbEntity pEnt)
654
            {
655
                OdDbPolygonMesh pPoly = (OdDbPolygonMesh)pEnt;
656
                Console.WriteLine("<{0}> {1}", pPoly.isA().name(), pPoly.getDbHandle().ToString());
657
                Console.WriteLine("m Size = {0}", pPoly.mSize().ToString());
658
                Console.WriteLine("m-Closed = {0}", pPoly.isMClosed().ToString());
659
                Console.WriteLine("m Surface Density = {0}", pPoly.mSurfaceDensity().ToString());
660
                Console.WriteLine("n Size = {0}", pPoly.nSize().ToString());
661
                Console.WriteLine("n-Closed = {0}", pPoly.isNClosed().ToString());
662
                Console.WriteLine("n Surface Density = {0}", pPoly.nSurfaceDensity().ToString());
663
                /********************************************************************/
664
                /* dump vertices                                                    */
665
                /********************************************************************/
666
                OdDbObjectIterator pIter = pPoly.vertexIterator();
667

    
668
                int vertexCount = 0;
669
                for (; !pIter.done(); pIter.step())
670
                {
671
                    OdDbPolygonMeshVertex pVertex = pIter.entity() as OdDbPolygonMeshVertex;
672
                    if (pVertex != null)
673
                    {
674
                        Console.WriteLine("<{0}> {1}", pVertex.isA().name(), vertexCount++);
675
                        Console.WriteLine("Handle = {0}", pVertex.getDbHandle().ToString());
676
                        Console.WriteLine("Position = {0}", pVertex.position().ToString());
677
                        Console.WriteLine("Type = {0}", pVertex.vertexType().ToString());
678
                    }
679
                }
680

    
681
                dumpEntityData(pPoly);
682
            }
683
        };
684
        static void dumpEntityData(OdDbEntity pEnt)
685
        {
686
            OdGeExtents3d extents = new OdGeExtents3d();
687
            if (OdResult.eOk == pEnt.getGeomExtents(extents))
688
            {
689
                Console.WriteLine("Min Extents = {0}", extents.minPoint().ToString());
690
                Console.WriteLine("Max Extents = {0}", extents.maxPoint().ToString());
691
            }
692
            Console.WriteLine("Layer = {0}", pEnt.layer());
693
            Console.WriteLine("Color Index = {0}", pEnt.colorIndex());
694
            Console.WriteLine("Color = {0}", pEnt.color().ToString());
695
            Console.WriteLine("Linetype = {0}", pEnt.linetype());
696
            Console.WriteLine("LTscale = {0}", pEnt.linetypeScale());
697
            Console.WriteLine("Lineweight = {0}", pEnt.lineWeight());
698
            Console.WriteLine("Plot Style = {0}", pEnt.plotStyleName());
699
            Console.WriteLine("Transparency Method = {0}", pEnt.transparency().method().ToString());
700
            Console.WriteLine("Visibility = {0}", pEnt.visibility().ToString());
701
            Console.WriteLine("Planar = {0}", pEnt.isPlanar());
702

    
703
            OdGePlane plane = new OdGePlane();
704
            Planarity planarity = Planarity.kNonPlanar;
705
            pEnt.getPlane(plane, out planarity);
706
            Console.WriteLine("Planarity = {0}", planarity);
707
            if (pEnt.isPlanar())
708
            {
709
                OdGePoint3d origin = new OdGePoint3d();
710
                OdGeVector3d uAxis = new OdGeVector3d();
711
                OdGeVector3d vAxis = new OdGeVector3d();
712
                plane.get(origin, uAxis, vAxis);
713
                Console.WriteLine("Origin = {0}", origin.ToString());
714
                Console.WriteLine("u-Axis = {0}", uAxis.ToString());
715
                Console.WriteLine("v-Axis = {0}", vAxis.ToString());
716
            }
717
        }
718
        public static double OdaToRadian(double deg) { return (deg) * Math.PI / 180.0; }
719
        void dumpAbstractViewTableRecord(OdDbAbstractViewTableRecord pView)
720
        {
721
            MemoryTransaction mTr = MemoryManager.GetMemoryManager().StartTransaction();
722

    
723
            /*********************************************************************/
724
            /* Dump the AbstractViewTableRecord                                  */
725
            /*********************************************************************/
726
            Console.WriteLine("Back Clip Dist = {0}", (pView.backClipDistance()));
727
            Console.WriteLine("Back Clip Enabled = {0}", (pView.backClipEnabled()));
728
            Console.WriteLine("Front Clip Dist = {0}", (pView.frontClipDistance()));
729
            Console.WriteLine("Front Clip Enabled = {0}", (pView.frontClipEnabled()));
730
            Console.WriteLine("Front Clip at Eye = {0}", (pView.frontClipAtEye()));
731
            Console.WriteLine("Elevation = {0}", (pView.elevation()));
732
            Console.WriteLine("Height = {0}", (pView.height()));
733
            Console.WriteLine("Width = {0}", (pView.width()));
734
            Console.WriteLine("Lens Length = {0}", (pView.lensLength()));
735
            Console.WriteLine("Render Mode = {0}", (pView.renderMode()));
736
            Console.WriteLine("Perspective = {0}", (pView.perspectiveEnabled()));
737

    
738
            OrthographicView orthoUCS;
739
            Console.WriteLine("UCS Orthographic = {0}", (pView.isUcsOrthographic(out orthoUCS)));
740
            Console.WriteLine("Orthographic UCS = {0}", (orthoUCS));
741
            if (pView.isUcsOrthographic(out orthoUCS))
742
            {
743
                OdGePoint3d origin = new OdGePoint3d();
744
                OdGeVector3d xAxis = new OdGeVector3d();
745
                OdGeVector3d yAxis = new OdGeVector3d();
746
                pView.getUcs(origin, xAxis, yAxis);
747
                Console.WriteLine("UCS Origin = {0}", (origin));
748
                Console.WriteLine("UCS x-Axis = {0}", (xAxis));
749
                Console.WriteLine("UCS y-Axis = {0}", (yAxis));
750
            }
751
            Console.WriteLine("Target = {0}", (pView.target()));
752
            Console.WriteLine("View Direction = {0}", (pView.viewDirection()));
753
            MemoryManager.GetMemoryManager().StopTransaction(mTr);
754
        }
755
        public OdGePoint3d ScreenPointToWorld(Point point)
756
        {
757
            OdGePoint3d odaPoint = ScreenPointToWorld(mHelper, point.X, point.Y);
758
            return odaPoint;
759
        }
760

    
761
        public OdGePoint3d ScreenPointToWorldTest(Point point)
762
        {
763
            OdGePoint3d pt = ScreenPointToWorld(mHelper, point.X, point.Y);
764
            pt = pt.transformBy(mHelper.activeView().worldToDeviceMatrix());
765
            return pt;
766
        }
767
        public OdGePoint3d SelectionPointsByPointOnScreen(double x, double y)
768
        {
769
            OdGePoint3d pt = ScreenPointToWorld(mHelper, x, y);
770
            pt = pt.transformBy(mHelper.activeView().worldToDeviceMatrix());
771
            return pt;
772
        }
773

    
774
        public void DrawAreaTest(Image bb, Point startPoint, Point endPoint)
775
        {
776
            if (pView != null)
777
            {
778
                Lock();
779
                using (OdDbTrace l = OdDbTrace.createObject())
780
                {
781
                    /*
782
                    MemoryTransaction mTr = MemoryManager.GetMemoryManager().StartTransaction(Thread.CurrentThread.ManagedThreadId);
783
                    
784

    
785
                    OdDbViewportTable pTable = (OdDbViewportTable)TDDatabase.getViewportTableId().safeOpenObject();
786
                    OdDbSymbolTableIterator pIter = pTable.newIterator();
787
                    for (pIter.start(); !pIter.done(); pIter.step())
788
                    {
789
                        OdDbViewportTableRecord pRecord = (OdDbViewportTableRecord)pIter.getRecordId().safeOpenObject();
790
                        dumpAbstractViewTableRecord(pRecord); 
791
                    }
792
                    MemoryManager.GetMemoryManager().StopTransaction(mTr);
793
                    */
794

    
795
                    OdGePoint3d ptS = ScreenPointToWorld(mHelper, startPoint.X, startPoint.Y);
796
                    OdGePoint3d ptS1 = ScreenPointToWorld(mHelper, startPoint.X, endPoint.Y);
797
                    OdGePoint3d ptE = ScreenPointToWorld(mHelper, endPoint.X, endPoint.Y);
798
                    OdGePoint3d ptE1 = ScreenPointToWorld(mHelper, endPoint.X, startPoint.Y);
799

    
800
                    l.setPointAt(0, ptS1);
801
                    l.setPointAt(1, ptS);
802
                    l.setPointAt(2, ptE);
803
                    l.setPointAt(3, ptE1);
804

    
805
                    OdDbBlockTableRecord model = (OdDbBlockTableRecord)TDDatabase.getModelSpaceId().safeOpenObject(OpenMode.kForWrite);
806
                    model.appendOdDbEntity(l);
807

    
808
                }
809

    
810
                mDevice.invalidate();
811
                mDevice.update(new OdGsDCRect(0, (int)NewSize.Width, (int)NewSize.Height, 0));//mDevice.update();
812
                UpdateDirtyRect();
813
                Unlock();
814
            }
815
        }
816
        private void Update1() // export to pdf
817
        {
818
            OdRxModule mod = Globals.odrxDynamicLinker().loadApp("TD_PdfExport");
819
            if (null == mod)
820
            {
821
                MessageBox.Show("Failed to load PDF export module", "Error");
822
            }
823

    
824
            Microsoft.Win32.SaveFileDialog dlgPdf = new Microsoft.Win32.SaveFileDialog();
825
            dlgPdf.DefaultExt = ".pdf";
826
            dlgPdf.Title = "PDF Export default";
827
            dlgPdf.Filter = "PDF files|*.pdf";
828
            // Display SaveFileDialog by calling ShowDialog method 
829
            if (dlgPdf.ShowDialog() != true)
830
            {
831
                return;
832
            }
833

    
834
            using (PDFExportParams Params = new PDFExportParams())
835
            {
836
                OdGsPageParams pParams = new OdGsPageParams();
837

    
838
                Params.setDatabase(TDDatabase);
839
                Params.setVersion(PDFExportParams.PDFExportVersions.kPDFv1_5);
840

    
841
                using (OdStreamBuf file = Globals.odrxSystemServices().createFile(dlgPdf.FileName, FileAccessMode.kFileWrite, FileShareMode.kShareDenyNo, FileCreationDisposition.kCreateAlways))
842
                {
843
                    Params.setOutput(file);
844
                    Params.setExportFlags(
845
                                 (PDFExportParams.PDFExportFlags.kEmbededTTF) |
846
                                 (PDFExportParams.PDFExportFlags.kSHXTextAsGeometry) | // no in sample
847
                                                                                       //(PDFExportParams.PDFExportFlags.kTTFTextAsGeometry) | // no in sample
848
                                 (PDFExportParams.PDFExportFlags.kSimpleGeomOptimization) |
849
                                 (PDFExportParams.PDFExportFlags.kZoomToExtentsMode) |
850
                                 (PDFExportParams.PDFExportFlags.kEnableLayers) | // under condition
851
                                 (PDFExportParams.PDFExportFlags.kIncludeOffLayers) | // under condition
852
                                    (PDFExportParams.PDFExportFlags.kUseHLR) |
853
                                    (PDFExportParams.PDFExportFlags.kFlateCompression) |
854
                                    (PDFExportParams.PDFExportFlags.kASCIIHexEncoding) |
855
                                    (PDFExportParams.PDFExportFlags.kExportHyperlinks));
856

    
857
                    Params.setTitle(dlgPdf.Title);
858
                    Params.setAuthor("WpfSample2");
859
                    Params.setSubject("WpfSample2");
860
                    Params.setKeywords("WpfSample2");
861
                    Params.setCreator("WpfSample2");
862
                    Params.setProducer("WpfSample2");
863

    
864
                    UInt32[] CurPalette = AllPalettes.getLightPalette();// OdDgColorTable.currentPalette(CurDb);
865
                    CurPalette[255] = 0x00ffffff; // the same as ODRGB(255, 255, 255); in the similar C++ code extract
866
                                                  //OdDbColorTable.correctPaletteForWhiteBackground(CurPalette);
867

    
868
                    Params.Palette = CurPalette;
869
                    Params.setBackground(CurPalette[0]);
870

    
871
                    /*if (dlgPdf.m_Layouts == 1) // all
872
                    {
873
                      OdDbModelTable pModelTable = TDDatabase.getModelTable();
874
                      if (null != pModelTable)
875
                      {
876
                        OdDgElementIterator pIter = pModelTable.createIterator();
877
                        for (; !pIter.done(); pIter.step())
878
                        {
879
                          OdDgModel pModel = OdDgModel.cast(pIter.item().openObject());
880
                          if (null != pModel)
881
                          {
882
                            Params.layouts.Add(pModel.getName());
883
                          }
884
                        }
885
                      }
886
                    }*/
887

    
888
                    /*Params.layouts.Add(TDDatabase.findActiveLayout(true));
889

    
890
                    UInt32 nPages = (UInt32)(1 > Params.layouts.Count ? 1 : Params.layouts.Count);
891

    
892
                    OdGsPageParams pageParams = new OdGsPageParams();
893
                    pageParams.set(210, 295);
894
                    Params.pageParams.resize(nPages);*/
895
                    PdfExportModule module = new PdfExportModule(OdRxModule.getCPtr(mod).Handle, false); //= PdfExportModule.cast();
896
                    OdPdfExport exporter = module.create();
897
                    UInt32 errCode = exporter.exportPdf(Params);
898

    
899
                    if (errCode != 0)
900
                    {
901
                        String errMes = exporter.exportPdfErrorCode(errCode);
902
                        String str;
903
                        str = string.Format("Error number : {0}. \n {1}", errCode, errMes);
904

    
905
                        if (errCode == 0x10008)
906
                        {
907
                            str += "\nPlease enable Zoom to extents check box or\ndefine page parameters for layout in page setup dialog.";
908
                        }
909

    
910
                        MessageBox.Show("PDF error", str);
911
                    }
912
                }
913
            }
914
        }
915
        private void Update2()
916
        {
917
            Lock();
918
            mDevice.update(new OdGsDCRect(0, (int)NewSize.Width, (int)NewSize.Height, 0));//mDevice.update();
919
            UpdateDirtyRect();
920
            Unlock();
921
        }
922
        private void Update3()
923
        {
924
            Lock();
925
            mDevice.invalidate();
926
            UpdateDirtyRect();
927
            Unlock();
928
        }
929
        public void ClearPoint()
930
        {
931
            if (pView != null)
932
            {
933
                Lock();
934

    
935
                if (startOdDbObjectId != null)
936
                {
937
                    OdDbObject obj = startOdDbObjectId.safeOpenObject(OpenMode.kForWrite);
938
                    obj.erase();
939
                    startOdDbObjectId = null;
940
                }
941
                if (endOdDbObjectId != null)
942
                {
943
                    OdDbObject obj = endOdDbObjectId.safeOpenObject(OpenMode.kForWrite);
944
                    obj.erase();
945
                    endOdDbObjectId = null;
946
                }
947
                //OdDbBlockTableRecord btr = (OdDbBlockTableRecord)TDDatabase.getModelSpaceId().safeOpenObject(OpenMode.kForWrite);
948
                //OdDbObjectIterator itr = btr.newIterator();
949

    
950

    
951
                //for (; !itr.done(); itr.step())
952
                //{
953
                //    OdDbEntity ent = (OdDbEntity)itr.objectId().openObject(OpenMode.kForWrite);
954
                //    if (ent.Equals(startPolyLine.objectId()))
955
                //    {
956
                //        ent.erase();
957
                //    }
958
                //    //if (itr.objectId().objectClass() is typeof(OdRxClass.SwigDelegateOdRxClass_1()))
959
                //    //if(itr.objectId().objectClass() is OdRxClass.(typeof(Circle))
960
                //    //if 라인
961
                //    //OdDbEntity ent = (OdDbEntity)itr.objectId().safeOpenObject();
962
                //    //itr.entity(OpenMode.kForWrite, true);
963
                //    //ent.erase();
964
                //}
965
            }
966

    
967
            mDevice.invalidate();
968
            mDevice.update(new OdGsDCRect(0, (int)NewSize.Width, (int)NewSize.Height, 0));//mDevice.update();
969
            //UpdateDirtyRect();
970
            Unlock();
971
        }
972

    
973
    }
974
    public class CustomServices : ExHostAppServices
975
    {
976
        public override string fileDialog(int flags, string dialogCaption, string defExt, string defFilename, string filter)
977
        {
978
            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
979

    
980
            dlg.DefaultExt = defExt;
981
            dlg.Title = dialogCaption;
982
            dlg.FileName = defFilename;
983
            dlg.Filter = filter;
984
            // Display SaveFileDialog by calling ShowDialog method 
985
            if (dlg.ShowDialog() == true)
986
            {
987
                return dlg.FileName;
988
            }
989
            throw new OdEdCancel();
990
        }
991
    }
992
}
클립보드 이미지 추가 (최대 크기: 500 MB)