프로젝트

일반

사용자정보

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

markus / KCOM / Controls / TeighaD3DImageResult.cs @ 90a0bfa9

이력 | 보기 | 이력해설 | 다운로드 (41.7 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 TeighaD3DImageResult : 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
        public OdRxObject odRxObjectSurface = null;
50
        public 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 TeighaD3DImageResult()
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
                //surfaceVariantValue = null;
563
    }
564
        }
565

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

    
573
            if (val.isErased())
574
            {
575
                return "Erased";
576
            }
577

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

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

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

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

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

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

    
637
                    OdDbBlockTableRecord model = (OdDbBlockTableRecord)TDDatabase.getModelSpaceId().safeOpenObject(OpenMode.kForWrite);
638

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

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

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

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

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

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

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

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

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

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

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

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

    
807
                    OdDbBlockTableRecord model = (OdDbBlockTableRecord)TDDatabase.getModelSpaceId().safeOpenObject(OpenMode.kForWrite);
808
                    model.appendOdDbEntity(l);
809

    
810
                }
811

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

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

    
836
            using (PDFExportParams Params = new PDFExportParams())
837
            {
838
                OdGsPageParams pParams = new OdGsPageParams();
839

    
840
                Params.setDatabase(TDDatabase);
841
                Params.setVersion(PDFExportParams.PDFExportVersions.kPDFv1_5);
842

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

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

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

    
870
                    Params.Palette = CurPalette;
871
                    Params.setBackground(CurPalette[0]);
872

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

    
890
                    /*Params.layouts.Add(TDDatabase.findActiveLayout(true));
891

    
892
                    UInt32 nPages = (UInt32)(1 > Params.layouts.Count ? 1 : Params.layouts.Count);
893

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

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

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

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

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

    
952

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

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

    
975
    }
976
   
977
}
클립보드 이미지 추가 (최대 크기: 500 MB)