拜托了————请给一个创建自动化服务器例子 ( 积分: 100 )

  • 主题发起人 主题发起人 inessence
  • 开始时间 开始时间
I

inessence

Unregistered / Unconfirmed
GUEST, unregistred user!
AutoCAD编程,用户交互界面打算用Delphi制作,然后用VisualLISP调用,正在学习COM,急需答疑解惑。
关于有窗体界面的自动化服务器,能不能给一个简单的例子的代码。
功能:控制器传一个integer给服务器,这个integer在服务器窗体的编辑框显示出来,用户可以更改,按确定按钮后,将更改后结果返回控制器。
拜托,一定把如何建立控制器的步骤讲一下,如哪一步用new->ActiveX的哪一个向导
 
AutoCAD编程,用户交互界面打算用Delphi制作,然后用VisualLISP调用,正在学习COM,急需答疑解惑。
关于有窗体界面的自动化服务器,能不能给一个简单的例子的代码。
功能:控制器传一个integer给服务器,这个integer在服务器窗体的编辑框显示出来,用户可以更改,按确定按钮后,将更改后结果返回控制器。
拜托,一定把如何建立控制器的步骤讲一下,如哪一步用new->ActiveX的哪一个向导
 
建议你好好翻翻书,入门的东西,你先大致了解一下,很容易的,这里又不是手把手教你学delphi论坛,几分钟做好的事情,我要是打字给你,恐怕都得半小时,你还未必就明白呢。多动动手嘛。
你到底要要控制器的建立步骤,还是服务器的建立步骤啊,搞清楚再问吧。
 
D5开发人员指南上有例子和介绍[:(]
 
我也在学AutoCAD的二次开发,我们老师非要用VB开发,我自己想用Dephi作
用Dephi还要调用VisualLISP吗?我试过,不用啊!
我作的例子是一个四杆机构的运动模拟
代码如下:
————————————————————————
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls,comobj, StdCtrls,Math;

type
TForm1 = class(TForm)
Timer1: TTimer;
Button2: TButton;
Button3: TButton;
Button5: TButton;
Button6: TButton;
Edit1: TEdit;
procedure FormCreate(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
AutoCadApp,Document,LineIndex,LineIndex1,LineIndex2,LineIndex3,LineIndex4: OleVariant;
StartPoint,EndPoint,APoint,BPoint,CPoint,DPoint,EPoint,FPoint:Variant;
LineAngle,LineAngle1,LineAngle3,A,B,C:Extended;
L1,L2,L3,L4: Double ;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
try
AutoCadApp:=GetActiveOleObject('AutoCAD.application.16');
except
AutoCadApp:=CreateOleObject('AutoCad.application.16');
end;
AutoCadApp.Visible := True ;
AutoCadApp.Documents.Add ;//新建文档
Document:=AutoCadApp.ActiveDocument;//当前活动文档
APoint:=VarArrayCreate([0,2],varDouble);//定义四个点,用于画四个首尾相连的杆
BPoint:=VarArrayCreate([0,2],varDouble);
CPoint:=VarArrayCreate([0,2],varDouble);
DPoint:=VarArrayCreate([0,2],varDouble);
APoint[0]:=120;APoint[1]:=120;APoint[2]:=0;//四个点的坐标出初始化
BPoint[0]:=120;BPoint[1]:=130;BPoint[2]:=0;
CPoint[0]:=180;CPoint[1]:=170;CPoint[2]:=0;
DPoint[0]:=180;DPoint[1]:=120;DPoint[2]:=0;
end;
procedure TForm1.Button3Click(Sender: TObject);//Close按钮
begin
Document.Close;//关闭打开的文档
close; //退出Dephi程序
end;

procedure TForm1.Button5Click(Sender: TObject);//Forbar按钮,用于画四个杆
begin
LineIndex1:=Document.modelSpace.AddLine(APoint,BPoint);//画杆1,其实就是直线
LineIndex1.color:= 1 ;//杆1的颜色
LineIndex1.update;
LineIndex2:=Document.modelSpace.AddLine(BPoint,CPoint);
LineIndex2.color:= 2 ;
LineIndex2.update;
LineIndex3:=Document.modelSpace.AddLine(DPoint,CPoint);
LineIndex3.color:= 3;
LineIndex2.update;
LineIndex4:=Document.modelSpace.AddLine(DPoint,APoint);
LineIndex4.update;
L1:= LineIndex1.Length;//分别获得四个杆的长度
L2:= LineIndex2.Length;
L3:= LineIndex3.Length;
L4:= LineIndex4.Length;
end;

procedure TForm1.Button2Click(Sender: TObject);//Move按钮
begin
Timer1.Enabled:=True //定时器开始工作
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
LineIndex1.Rotate (APoint , 3.1415926/36) ; //第一个杆旋转10度
LineIndex1.UpDate; //更新CAD中的显示
LineAngle1:=LineIndex1.Angle;//获取杆1的角度,用于后面的计算
A:=L4-L1*cos(LineAngle1); //以下四行算出杆3的角度
B:=-1*L1*sin (LineAngle1);
C:=(A*A+B*B+L3*L3-L2*L2)/(2*L3);
LineAngle3:=2*arctan((B+sqrt(A*A+B*B-C*C))/(A-C));
LineAngle:=LineAngle3-LineIndex3.Angle;//获取杆3应转过的角度
LineIndex3.Rotate (DPoint , LineAngle) ;//杆3旋转
LineIndex2.StartPoint:=LineIndex1.EndPoint;//杆2的起点始终跟随杆1的终点
LineIndex2.EndPoint :=LineIndex3.EndPoint;//杆2的终点跟随杆3的终点
end;

procedure TForm1.Button6Click(Sender: TObject);//Stop按钮
begin
Timer1.Enabled:=False;//计时器停止
end;

end.

——————————————————————————
运行截图如下:
http://img46.photo.163.com/anhuihukai/7541457/__scale__1_151280470.jpg

PS:大富翁里怎么贴图啊[:D]
 
晕,上面那个图片太小了,这个是大图
http://img46.photo.163.com/anhuihukai/7541457/151280470.jpg
 
有E-MAIL吗???给你发个例子和我的经验。
 
楼上的给我发个吧
anhuihukai@163.com
 
▲包含窗体的进程内自动化服务器 (in-process Automation server,DLL)
by lazybones
第一步,建立ActiveX Library
File->New->Other->ActiveX Library,存盘取名inprocess
第二步,添加Automation Object
File->New->Other->Automation Object,CoClass Name取名getstring,选择Igetstring,单击New Property按钮旁边的下拉箭头,增加一个Read Only的属性thestring,Type为 BSTR,单击New Method,增加一个freeform方法,再单击New Method,增加一个showmodelform方法,单击Parameters标签,Name为str,Type为BSTR,刷新
第三步,添加窗体
File->New->Form,添加一个编辑框。全部保存
第四部,完善代码
首先需要在unit1中uses unit2,再完成代码,编译、注册

//ActiveX Library

library inprocess;

uses
ComServ,
inprocess_TLB in 'inprocess_TLB.pas',
Unit1 in 'Unit1.pas',
Unit2 in 'Unit2.pas' {Form2};

exports
DllGetClassObject,
DllCanUnloadNow,
DllRegisterServer,
DllUnregisterServer;

{$R *.TLB}

{$R *.RES}

begin
end.



//Automation Object

unit Unit1;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
ComObj, ActiveX, inprocess_TLB, StdVcl,unit2;

type
Tgetstring = class(TAutoObject, Igetstring)
protected
function Get_thestring: WideString; safecall;
procedure freeform; safecall;
procedure showmodelform(const str: WideString); safecall;

end;

implementation

uses ComServ;

function Tgetstring.Get_thestring: WideString;
begin
result:=form2.Edit1.Text;
end;

procedure Tgetstring.freeform;
begin
form2.Free;
end;

procedure Tgetstring.showmodelform(const str: WideString);
begin
form2:=TForm2.Create(nil);
form2.Edit1.Text:=str;
form2.ShowModal;//注意,和上一句顺序不能颠倒
end;

initialization
TAutoObjectFactory.Create(ComServer, Tgetstring, Class_getstring,
ciMultiInstance, tmApartment);
end.



//Automation Object(TLB)

unit inprocess_TLB;

// ************************************************************************ //
// WARNING
// -------
// The types declared in this file were generated from data read from a
// Type Library. If this type library is explicitly or indirectly (via
// another type library referring to this type library) re-imported, or the
// 'Refresh' command of the Type Library Editor activated while editing the
// Type Library, the contents of this file will be regenerated and all
// manual modifications will be lost.
// ************************************************************************ //

// PASTLWTR : 1.2
// File generated on 2005-7-23 23:17:31 from Type Library described below.

// ************************************************************************ //
// Type Lib: C:/Documents and Settings/Administrator/My Documents/Borland Studio Projects/ooo/inprocess.tlb (1)
// LIBID: {5B026064-6CED-4AEB-9AF4-7B246AF2518B}
// LCID: 0
// Helpfile:
// HelpString: inprocess Library
// DepndLst:
// (1) v2.0 stdole, (C:/WINDOWS/system32/stdole2.tlb)
// ************************************************************************ //
{$TYPEDADDRESS OFF} // Unit must be compiled without type-checked pointers.
{$WARN SYMBOL_PLATFORM OFF}
{$WRITEABLECONST ON}
{$VARPROPSETTER ON}
interface

uses Windows, ActiveX, Classes, Graphics, StdVCL, Variants;


// *********************************************************************//
// GUIDS declared in the TypeLibrary. Following prefixes are used:
// Type Libraries : LIBID_xxxx
// CoClasses : CLASS_xxxx
// DISPInterfaces : DIID_xxxx
// Non-DISP interfaces: IID_xxxx
// *********************************************************************//
const
// TypeLibrary Major and minor versions
inprocessMajorVersion = 1;
inprocessMinorVersion = 0;

LIBID_inprocess: TGUID = '{5B026064-6CED-4AEB-9AF4-7B246AF2518B}';

IID_Igetstring: TGUID = '{D2D01F47-D4EB-4D87-91F5-AF6600E8DFAF}';
CLASS_getstring: TGUID = '{A2E7576F-16DC-404C-8145-9F0F0BE50868}';
type

// *********************************************************************//
// Forward declaration of types defined in TypeLibrary
// *********************************************************************//
Igetstring = interface;
IgetstringDisp = dispinterface;

// *********************************************************************//
// Declaration of CoClasses defined in Type Library
// (NOTE: Here we map each CoClass to its Default Interface)
// *********************************************************************//
getstring = Igetstring;


// *********************************************************************//
// Interface: Igetstring
// Flags: (4416) Dual OleAutomation Dispatchable
// GUID: {D2D01F47-D4EB-4D87-91F5-AF6600E8DFAF}
// *********************************************************************//
Igetstring = interface(IDispatch)
['{D2D01F47-D4EB-4D87-91F5-AF6600E8DFAF}']
function Get_thestring: WideString; safecall;
procedure freeform; safecall;
procedure showmodelform(const str: WideString); safecall;
property thestring: WideString read Get_thestring;
end;

// *********************************************************************//
// DispIntf: IgetstringDisp
// Flags: (4416) Dual OleAutomation Dispatchable
// GUID: {D2D01F47-D4EB-4D87-91F5-AF6600E8DFAF}
// *********************************************************************//
IgetstringDisp = dispinterface
['{D2D01F47-D4EB-4D87-91F5-AF6600E8DFAF}']
property thestring: WideString readonly dispid 201;
procedure freeform; dispid 202;
procedure showmodelform(const str: WideString); dispid 203;
end;

// *********************************************************************//
// The Class Cogetstring provides a Create and CreateRemote method to
// create instances of the default interface Igetstring exposed by
// the CoClass getstring. The functions are intended to be used by
// clients wishing to automate the CoClass objects exposed by the
// server of this typelibrary.
// *********************************************************************//
Cogetstring = class
class function Create: Igetstring;
class function CreateRemote(const MachineName: string): Igetstring;
end;

implementation

uses ComObj;

class function Cogetstring.Create: Igetstring;
begin
Result := CreateComObject(CLASS_getstring) as Igetstring;
end;

class function Cogetstring.CreateRemote(const MachineName: string): Igetstring;
begin
Result := CreateRemoteComObject(MachineName, CLASS_getstring) as Igetstring;
end;

end.



// Form2

unit Unit2;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm2 = class(TForm)
Edit1: TEdit;
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation

{$R *.dfm}

end.



‘VBA控制器代码

Private Sub CommandButton1_Click()
Dim auto
Dim ret
Set auto = CreateObject("inprocess.getstring")
auto.showmodelform ("Welcom")
ret = auto.thestring
auto.freeform
MsgBox ret
End Sub

总结:
1、 曲折地实现了预想的功能。
2、 实验显示,在CreateObject之后,Show或ShowModel之前,对于控制器来说,服务器的窗体及控件不可用;而一旦ShowModel,进程则停留在服务器,只有在窗体关闭后,进程才回到控制器,总之,控制器不能直接通过服务器的属性来对其进行设定很不爽。
3、 下一课题是如何将数据库封装成服务器,在控制器和服务器之间传递复杂数据类型。
 
接受答案了.
 
补充改进
将 Form2.Create和Form2.ShowModel分开放到两个method中Create和ShowModel,调用时主程序先调用服务器的窗体生成方法,再利用服务器对象属性传递参数,然后再调用服务器的显示模态窗体方法。
主程序中调用顺序
Set auto = CreateObject("inprocess.getstring")
auto.Create
au.str="my string"
au.ShowModel
前提是在服务器里增加一个set_thestring函数
 
后退
顶部