新手入门必看:VectorDraw 常见问题整理大全(四)

VectorDraw Developer Framework(VDF)是一个用于应用程序可视化的图形引擎库。有了VDF提供的功能,您可以轻松地创建、编辑、管理、输出、输入和打印2D和3D图形文件。该库还支持许多矢量和栅格输入和输出格式,包括本地PDF和SVG导出。

VectorDraw web library (javascript)是一个矢量图形库。VectorDraw web library (javascript)不仅能打开CAD图纸,而且能显示任何支持HTML5标准平台上的通用矢量对象,如Windows,安卓,iOS和Linux。无需任何安装,VectorDraw web library (javascript)就可以运行在任何支持canvas标签和Javascript的主流浏览器(Chrome, Firefox, Safari, Opera, Dolphin, Boat等等)中。

一. 在包装器组件中使用新对象和功能

问:如何在包装器组件中使用新对象和功能?

答:包装器组件是一个COM ActiveX,可以在vd6和c ++ 6等环境中使用。我们已经实现了.tlb文件,这些文件可以添加到项目的引用中,并且可以为您的应用程序提供新组件的功能。

导出的tlb文件:VectorDraw.Serialize.tlbVectorDraw.Render.tlbVectorDraw.Professional.tlbVectorDraw.Geometry.tlbVectorDraw.Actions.tlbVdrawPro5.tlbvdrawi5.tlbvdPropertyGrid.tlbVdProControl.tlb

下面的代码示例实现了包装器组件,其中使用按钮我们添加了一个vdMtext对象,这是一个全新的对象,它在我们的新库中实现,并且在版本5中不存在。

对于这个实现,我们导入了VectorDraw.Geometry .tlb,VectorDraw.Professional.tlb以及vdrawi5.tlb

VB6代码示例:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

Private Sub Command1_Click()

'Get the document interface of VectorDraw FrameWork

   Dim doc As VectorDraw_Professional.vdDocument

   Set doc = VDraw1.ActiveDocument.WrapperObject

'Create a new Mtext object with VDF Interfaces

   Dim mtext As VectorDraw_Professional.vdMText

   Set mtext = New VectorDraw_Professional.vdMText

 

   'typecast the Mtext as vdbaseObject

   Dim baseobj As VectorDraw_Professional.vdBaseObject

   Set baseobj = mtext

   baseobj.SetUnRegisterDocument doc

   'typecast the Mtext as vdPrimary

   Dim primary As VectorDraw_Professional.vdPrimary

   Set primary = mtext

   primary.setDocumentDefaults

 

   'Create a gPoint object

   Dim pt As VectorDraw_Geometry.gPoint

   Set pt = New VectorDraw_Geometry.gPoint

   pt.SetValue 2, 3, 0

   'set values in some properties of mtext

   mtext.TextString = "VectorDraw Mtext using VDF Interfaces"

   Set mtext.InsertionPoint = pt

   mtext.BoxWidth = 12

   mtext.Height = 1#

 

   'typecast the Mtext as vdFigure

   Dim fig As VectorDraw_Professional.vdFigure

   Set fig = mtext

   'add mtext in the collection entities table

   doc.ActiveLayOut.entities.AddItem fig

 

   primary.Update

   fig.Invalidate

   VDraw1.CommandAction.Zoom "e", 0, 0

End Sub

C ++代码示例:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

void Cvd6WrapperDlg::OnBnClickedMtext()

{

//Get the document interface of VectorDraw FrameWork

   VectorDraw_Professional::IvdDocumentPtr doc =       m_vdraw.GetActiveDocument().GetWrapperObject();

 

//Create a new Mtext object with VectorDraw FrameWork Interfaces

   VectorDraw_Professional::IvdMTextPtr    mtext(__uuidof(VectorDraw_Professional::vdMText));

 

//typecast the Mtext as vdbaseObject

   VectorDraw_Professional::IvdBaseObjectPtr baseobj =    (VectorDraw_Professional::IvdBaseObjectPtr)mtext;

   baseobj->SetUnRegisterDocument(doc);

 

//typecast the Mtext as vdPrimary

   VectorDraw_Professional::IvdPrimaryPtr primary = (VectorDraw_Professional::IvdPrimaryPtr)mtext;

   primary->setDocumentDefaults();

 

//Create a gPoint object;

   VectorDraw_Geometry::IgPointPtr pt(__uuidof(VectorDraw_Geometry::gPoint));

pt->SetValue(2,3,0);

 

//set values in some properties of mtext

   mtext->PutRefInsertionPoint(pt);

   mtext->PutTextString("Vectordraw Mtext using VectorDrawFramework Interfaces");

   mtext->PutBoxWidth(12);

   mtext->PutHeight(1.0);

 

//typecast the Mtext as vdFigure

   VectorDraw_Professional::IvdFigurePtr figure = (VectorDraw_Professional::IvdFigurePtr)mtext;

 

//add mtext in the collection entities table

   doc->ActiveLayOut->Entities->AddItem(figure);

   primary->Update();

   figure->Invalidate();

  m_vdraw.GetCommandAction().Zoom(COleVariant(_T("e")),COleVariant(),COleVariant());

}

Delphi 7代码示例:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, SHDocVw, AxCtrls, OleCtrls,

VDrawLib5_TLB, VDrawI5_TLB, VectorDraw_Professional_TLB, VectorDraw_Geometry_TLB;

 

 

 

procedure TForm1.Button1Click(Sender: TObject);

var

   mtext : VectorDraw_Professional_TLB.IvdMText;

   pt : VectorDraw_Geometry_TLB.Igpoint;

   doc : VectorDraw_Professional_TLB.IvdDocument;

   primary : VectorDraw_Professional_TLB.IvdPrimary;

   baseobject : VectorDraw_Professional_TLB.IvdBaseObject;

 

begin

   doc := vdrawpro.ActiveDocument.WrapperObject as VectorDraw_Professional_TLB.IvdDocument;

   pt := VectorDraw_Geometry_TLB.CogPoint.Create();

   pt.SetValue(2,3,0);

   mtext := VectorDraw_Professional_TLB.CovdMText.Create();

   baseobject := mtext as VectorDraw_Professional_TLB.IvdBaseObject;

   baseobject.SetUnRegisterDocument(doc);

   primary:=mtext as VectorDraw_Professional_TLB.IvdPrimary;

   primary.setDocumentDefaults();

   mtext.TextString := 'VectorDraw Mtext using VDF Interfaces';

   mtext.InsertionPoint := pt;

   mtext.BoxWidth := 12;

   mtext.Height := 1;

   doc.ActiveLayOut.entities.AddItem(mtext as VectorDraw_Professional_TLB.IvdFigure);

   vdrawpro.CommandAction.Zoom('E',0,0);

end;

procedure TForm1.Button2Click(Sender: TObject);

var vdGPts : VectorDraw_Geometry_TLB.Igpoints;

  doc : VectorDraw_Professional_TLB.IvdDocument;

  primary : VectorDraw_Professional_TLB.IvdPrimary;

  baseobject : VectorDraw_Professional_TLB.IvdBaseObject;

  vdGPt : VectorDraw_Geometry_TLB.Igpoint;

  GS : VectorDraw_Professional_TLB.IvdGroundSurface;

  I : integer;

 

begin

  vdraw1.DisplayFrames:=63;

  vdraw1.StatusBar:=true;

  vdraw1.StatusBarMenu:=true;

  vdraw1.EnableAutoGripOn:=true;

 

  doc := vdraw1.ActiveDocument.WrapperObject as VectorDraw_Professional_TLB.IvdDocument;

 

  vdGPts := VectorDraw_Geometry_TLB.CogPoints.Create();

  for I:=1 to 8 do // create a ground sourface from 8 points

  begin

    vdGPt := VectorDraw_Geometry_TLB.CogPoint.Create();

    vdGPts.Add(vdGpt);

  end;

 

  vdGPTs.Item[0].SetValue(2,3,0);

  vdGPTs.Item[1].SetValue(2,5,1);

  vdGPTs.Item[2].SetValue(4,5,1.5);

  vdGPTs.Item[3].SetValue(4,3,1);

  vdGPTs.Item[4].SetValue(7,3,2);

  vdGPTs.Item[5].SetValue(7,5,0.5);

  vdGPTs.Item[6].SetValue(9,3,0.3);

  vdGPTs.Item[7].SetValue(9,5,0.5);

 

  GS := VectorDraw_Professional_TLB.CovdGroundSurface.Create();

  baseobject := GS as VectorDraw_Professional_TLB.IvdBaseObject;

  baseobject.SetUnRegisterDocument(doc);

  primary:=GS as VectorDraw_Professional_TLB.IvdPrimary;

  primary.setDocumentDefaults();

  GS.Points := vdGPts;

  GS.MeshSize := 0.1;

  GS.DispMode := DisplayMode_Mesh;//DisplayMode_Triangle;

  doc.ActiveLayOut.entities.AddItem(GS as VectorDraw_Professional_TLB.IvdFigure);

  vdraw1.CommandAction.View3D('VISE');

  vdraw1.CommandAction.Zoom('E',0,0);

end;

二. 计算用户在3D中单击多边形的点

问:如何计算用户在3D中单击多边形的点?

答:在下面的代码示例中,我们要求用户单击一个点。我们检查用户是否单击了一个多边形。从这一点开始,我们计算出一条从用户的"eye"和内部开始的线。我们计算此线与找到的多面的所有面的交点。然后我们排除所有不属于脸部的点,我们选择更接近用户“eye”的点。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

Important Functions Used : Intersection3DSegmentPlane,PointInTriangle, CalculateExtrution  can be found in VectorDraw.Geometry.Globals.

 

private void button4_Click(object sender, EventArgs e)

{

vdDocument doc = vdFramedControl1.BaseControl.ActiveDocument;

gPoint userpt = null;

//Get a point from the user

VectorDraw.Actions.StatusCode s= doc.ActionUtility.getUserPointDCS(out userpt);

if (s == VectorDraw.Actions.StatusCode.Success)

{

//Find the polyface the user clicked.

gPoint tmppt = doc.View2PixelMatrix.Transform(userpt);

Point screenpt = new Point((int)tmppt.x, (int)tmppt.y);

//This function it is recommended not to go to the Mouse Down event(it might slow down your application)

//It is recommended to create a button function to do this like this one.

vdEntities ents = doc.Select3d(screenpt, doc.GlobalRenderProperties.PickSize);

if (ents.Count != 0)

{

vdPolyface polyface = ents[0] as vdPolyface;

if (polyface != null)

{

//Calculate the biggest z value of the polyface.

double val = 0.0;

foreach (gPoint var in polyface.VertexList)

{

if (val < var.z) val = var.z;

}

//Calculate the line that starts from the point that the user clicked and goes "inside the screen" to intersect the face.

gPoint world = doc.View2WorldMatrix.Transform(userpt);

gPoint linept1 = world - (val * 10.0) * doc.ActiveLayOut.ViewDir;

gPoint linept2 = world + (val*10.0)*doc.ActiveLayOut.ViewDir;

gPoints FoundPoints = new gPoints();

int count = 1;

foreach (int var in polyface.FaceList)

{

count++;

if (count % 5 == 0)

{

//4 points of the face.

gPoint pt1 = polyface.VertexList[polyface.FaceList[count - 5] - 1];

gPoint pt2 = polyface.VertexList[polyface.FaceList[count - 4] - 1];

gPoint pt3 = polyface.VertexList[polyface.FaceList[count - 3] - 1];

gPoint pt4 = polyface.VertexList[polyface.FaceList[count - 2] - 1];

//Calculate the perpedicular vector of the plane of the face.

Vector v = new Vector();

v.CalculateExtrution(pt1, pt2,pt3);

//Calculate the intersection point of the plane with the above calculated line

gPoint ret1 = new gPoint();

int j = VectorDraw.Geometry.Globals.Intersection3DSegmentPlane(linept1, linept2, v, pt1, out ret1);

if (j != 0)

{

bool triangle1 = VectorDraw.Geometry.Globals.PointInTriangle(doc.World2ViewMatrix.Transform (ret1),doc.World2ViewMatrix.Transform ( pt1), doc.World2ViewMatrix.Transform (pt2), doc.World2ViewMatrix.Transform (pt3));

bool triangle2 = VectorDraw.Geometry.Globals.PointInTriangle(doc.World2ViewMatrix.Transform (ret1),doc.World2ViewMatrix.Transform ( pt1),doc.World2ViewMatrix.Transform ( pt3), doc.World2ViewMatrix.Transform (pt4));

if (triangle1 || triangle2) //If the point belongs to any of the triangles then belongs to the face.

{

//Add the found point to this list.

FoundPoints.Add(ret1);

}

}

}

}

//Debug Add a vdPoint for each point found.

foreach (gPoint var in FoundPoints)

{

vdPoint vdpt = new vdPoint();

vdpt.SetUnRegisterDocument(doc);

vdpt.setDocumentDefaults();

vdpt.PenColor.ColorIndex = 2;

vdpt.InsertionPoint = var;

doc.ActiveLayOut.Entities.AddItem(vdpt);

vdpt.Invalidate();

}

//This is for debug purposes we will add the calculated line and the Found Point.

vdLine line = new vdLine(linept1, linept2);

line.SetUnRegisterDocument(doc);

line.setDocumentDefaults();

line.PenColor.ColorIndex = 0;

doc.ActiveLayOut.Entities.AddItem(line);

line.Invalidate();

 

//Now we will choose the point that has the less distance from linept2

int index = 0;

int indexFound = 0;

double dist = linept1.Distance3D(linept2); //We start from the biggest distance

foreach (gPoint var in FoundPoints)

{

double dist1 = var.Distance3D(linept2);

if (dist1 < dist)

{

indexFound = index;

dist = dist1;

}

index++;

}

//Add the Found point with different color.

vdPoint vdpt1 = new vdPoint();

vdpt1.SetUnRegisterDocument(doc);

vdpt1.setDocumentDefaults();

vdpt1.PenColor.ColorIndex = 3;

vdpt1.InsertionPoint = FoundPoints[indexFound];

doc.ActiveLayOut.Entities.AddItem(vdpt1);

vdpt1.Invalidate();

}

}

}

}

未完待续......

转载于:https://my.oschina.net/u/3876903/blog/2250804

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值