home *** CD-ROM | disk | FTP | other *** search
- { *****************************************************************************
- Module: MAIN.PAS
-
- Purpose: Simple coding example that codes a dialog box allowing the
- user to work with blocks and attributes in a VDraft drawing.
-
- Programmer(s): SoftSource programming team
- Robert Cheek
-
- History: ~~~ 1997 ~~~
- Apr 01 - Created (ported from Visual Basic code developed by
- SoftSource).
- May 12 - Completed port.
-
- ***************************************************************************** }
- unit Main;
- interface
-
- { -----------------------------------------------------------------------------
- Other Pascal code units used by the program
- NOTE: The key clause is "OleAuto" which Delphi requires for working with
- other OLE Automation Servers.
- ----------------------------------------------------------------------------- }
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls, OleAuto;
-
- { -----------------------------------------------------------------------------
- Types and class definitions
- ----------------------------------------------------------------------------- }
- type
- TfrmAttributes = class( TForm )
- // Controls on the form
- btnGetBlocks: TButton;
- btnQuit: TButton;
- lblFileName: TLabel;
- btnSelect: TButton;
- lstBlocks: TListBox;
- btnGetAttributes: TButton;
- btnOutput: TButton;
- btnEditAttributes: TButton;
- btnZoom: TButton;
- lstAttributes: TListBox;
- lblInsertCount: TLabel;
-
- // Methods for the controls
- procedure FormCreate( Sender: TObject );
- procedure FormClose( Sender: TObject; var Action: TCloseAction );
- procedure btnGetBlocksClick( Sender: TObject );
- procedure btnSelectClick( Sender: TObject );
- procedure lstBlocksClick( Sender: TObject );
- procedure btnGetAttributesClick( Sender: TObject );
- procedure btnOutputClick( Sender: TObject );
- procedure lstAttributesClick( Sender: TObject );
- procedure btnEditAttributesClick( Sender: TObject );
- procedure btnZoomClick( Sender: TObject );
- procedure btnQuitClick( Sender: TObject );
-
- private
- // Private declarations
- vDraft: Variant; // Handle to VDraft
- vDrawing: Variant; // Handle to active drawing
-
- public
- // Public declarations
-
- end;
-
- { -----------------------------------------------------------------------------
- Global variables
- ----------------------------------------------------------------------------- }
- var
- frmAttributes: TfrmAttributes; // Instance of the form
-
- implementation
- {$R *.DFM}
-
- { -----------------------------------------------------------------------------
- Method: FormCreate( Sender: TObject )
-
- Parameters: Object or control that triggered the event
-
- Returns: Nothing
-
- Purpose: Event method called by Windows when the form is being created.
- ----------------------------------------------------------------------------- }
- procedure TfrmAttributes.FormCreate( Sender: TObject );
- begin
- // Set the size of the form
- ClientHeight := lblInsertCount.Top + lblInsertCount.Height + 8;
- ClientWidth := btnZoom.Left + btnZoom.Width + 4;
-
- // Clear the labels and disable some of the controls
- lblFileName.Caption := '';
- lblInsertCount.Caption := '';
- btnSelect.Enabled := False;
- btnGetAttributes.Enabled := False;
- btnOutput.Enabled := False;
- btnEditAttributes.Enabled := False;
- btnZoom.Enabled := False;
-
- // Start an instance of VDraft
- try
- VDraft := CreateOleObject( 'Vdraft.Application' );
- except
- on EOleError do begin
- // Give an error message and abort
- MessageDlg( 'Unable to establish communications with VDraft. Application cannot continue ...', mtError, [mbOK], 0 );
- Close;
- end;
- end;
- end;
-
- { -----------------------------------------------------------------------------
- Method: FormClose( Sender: TObject )
-
- Parameters: Object or control that triggered the event
-
- Returns: Nothing
-
- Purpose: Event method called by Windows when the form is being closed.
- ----------------------------------------------------------------------------- }
- procedure TfrmAttributes.FormClose( Sender: TObject; var Action: TCloseAction );
- begin
- // Shut down VDraft and free all memory
- VDraft := UnAssigned;
- Action := caFree;
- end;
-
- { -----------------------------------------------------------------------------
- Method: btnGetBlocksClick( Sender: TObject )
-
- Parameters: Object or control that triggered the event
-
- Returns: Nothing
-
- Purpose: Event method triggered when the user has clicked on the forms
- Get Blocks in Current Drawing button.
- ----------------------------------------------------------------------------- }
- procedure TfrmAttributes.btnGetBlocksClick( Sender: TObject );
- var
- crCursor: TCursor; // Handle to current (saved) cursor
- vBlocks: Variant; // Handle to block table
- iCount: Integer; // Generic loop counter
- iBlockCount: Integer; // Number of blocks in drawing
- begin
- // Let user know that we're working and clear block list
- crCursor := Screen.Cursor;
- Screen.Cursor := crHourglass;
- lstBlocks.Items.Clear;
-
- // Are there any active documents?
- if vDraft.Documents.Count = 0 then begin
- // Restore the cursor and exit the procedure
- Screen.Cursor := crCursor;
- Exit;
- end;
-
- // Get current drawing and display its name
- vDrawing := vDraft.ActiveDocument;
- lblFileName.Caption := vDrawing.FullName;
-
- // List all the blocks in the drawing
- vBlocks := vDrawing.Blocks;
- iBlockCount := vBlocks.Count;
- for iCount := 1 to iBlockCount do begin
- // CANNOT DO THIS IN DELPHI - Compiler thinks this is
- // a function call!
- // vBlock := vBlocks( iCount );
-
- // Don't list dimensions or hatches
- if vBlocks.Item( iCount ).IsAnonymous = False then
- lstBlocks.Items.Add( vBlocks.Item( iCount ) );
- end;
-
- // Restore the mouse pointer to normal
- Screen.Cursor := crCursor;
- end;
-
- { -----------------------------------------------------------------------------
- Method: btnSelectClick( Sender: TObject )
-
- Parameters: Object or control that triggered the event
-
- Returns: Nothing
-
- Purpose: Event method triggered when the user has clicked on the forms
- Select All Inserts of Block button.
- ----------------------------------------------------------------------------- }
- procedure TfrmAttributes.btnSelectClick( Sender: TObject );
- var
- crCursor: TCursor; // Handle to current (saved) cursor
- vBlock: Variant; // Handle to specified block
- vInserts: Variant; // Handle to specific block insertion
- vCommands: Variant; // Handle to commands
- vSelection: Variant; // Handle to selection set
- psBlockName: String; // Name of selected block
- iCount: Integer; // Generic counter
- begin
- // Set the cursor to an hourglass
- crCursor := Screen.Cursor;
- Screen.Cursor := crHourglass;
-
- // Get associated list of inserts from VDraft
- psBlockName := lstBlocks.Items.Strings[lstBlocks.ItemIndex];
- vBlock := vDrawing.Blocks.Item( psBlockName );
- vInserts := vBlock.Inserts;
-
- // Group all selections under one command
- vCommands := vDrawing.Commands;
- vCommands.Group( 'select.inserts' + psBlockName );
-
- // Select all the inserts
- vSelection := vDrawing.Selection;
- for iCount := 1 to vInserts.Count do
- vSelection.Add( vInserts.Item( iCount ) );
-
- // Restore the starting cursor
- Screen.Cursor := crCursor;
- end;
-
- { -----------------------------------------------------------------------------
- Method: lstBlocksClick( Sender: TObject )
-
- Parameters: Object or control that triggered the event
-
- Returns: Nothing
-
- Purpose: Event method triggered when the user has clicked the mouse
- pointer in the forms block names list box.
- ----------------------------------------------------------------------------- }
- procedure TfrmAttributes.lstBlocksClick( Sender: TObject );
- var
- vBlock: Variant; // Handle to specified block
- iCount: Integer; // Number of block insertions
- begin
- // Tell the user how many inserts of selected block there are
- vBlock := vDrawing.Blocks.Item( lstBlocks.Items.Strings[lstBlocks.ItemIndex] );
- iCount := vBlock.Inserts.Count;
- lblInsertCount.Caption := Format( '%d inserts of block %s in this drawing',
- [iCount, lstBlocks.Items.Strings[lstBlocks.ItemIndex]] );
-
- // Once they select a block, the buttons will do something
- btnSelect.Enabled := True;
- btnGetAttributes.Enabled := vBlock.HasAttributes;
- btnOutput.Enabled := False;
- lstAttributes.Items.Clear;
- btnEditAttributes.Enabled := False;
- btnZoom.Enabled := False;
- end;
-
- { -----------------------------------------------------------------------------
- Method: btnGetAttributesClick( Sender: TObject )
-
- Parameters: Object or control that triggered the event
-
- Returns: Nothing
-
- Purpose: Event method triggered when the user has clicked on the forms
- Get Attributes button.
- ----------------------------------------------------------------------------- }
- procedure TfrmAttributes.btnGetAttributesClick( Sender: TObject );
- var
- crCursor: TCursor; // Handle to current (saved) cursor
- psBlockName: String; // Name of block
- psOutput: String; // Attribute names to display
- vInserts: Variant; // Handle to specific block insertion
- vAttribs: Variant; // Handle to specific attribute set
- iAttribCount: Integer; // Count of associated attributes
- iCount1: Integer; // Generic counters
- iCount2: Integer;
- begin
- // Set the cursor to an hourglass
- crCursor := Screen.Cursor;
- Screen.Cursor := crHourglass;
-
- // Clear the attribute list box
- lstAttributes.Items.Clear;
- btnEditAttributes.Enabled := False;
- btnZoom.Enabled := False;
-
- // Get associated list of inserts from VDraft
- psBlockName := lstBlocks.Items.Strings[lstBlocks.ItemIndex];
- vInserts := vDrawing.Blocks.Item( psBlockName ).Inserts;
- if vInserts.Count = 0 then
- Exit;
-
- // Build up attribute list
- iAttribCount := vInserts.Item( 1 ).Attributes.Count;
- for iCount1 := 1 to vInserts.Count do begin
- vAttribs := vInserts.Item( iCount1 ).Attributes;
- psOutput := vAttribs.Item( 1 ).Text;
- for iCount2 := 2 to iAttribCount do
- psOutput := psOutput + ', ' + vAttribs.Item( iCount2 ).Text;
- lstAttributes.Items.Add( psOutput );
- end;
-
- // Allow output and restore the starting cursor
- btnOutput.Enabled := True;
- Screen.Cursor := crCursor;
- end;
-
- { -----------------------------------------------------------------------------
- Method: btnOutputClick( Sender: TObject )
-
- Parameters: Object or control that triggered the event
-
- Returns: Nothing
-
- Purpose: Event method triggered when the user has clicked on the forms
- Output button.
- ----------------------------------------------------------------------------- }
- procedure TfrmAttributes.btnOutputClick( Sender: TObject );
- // Dim inserts, attribs As Object
- var
- cdOpen: TOpenDialog; // Open file class
- crCursor: TCursor; // Handle to current (saved) cursor
- vInserts: Variant; // Handle to block insertions
- vAttribs: Variant; // Handle to blocks attributes
- psBlockName: String; // Name of specified block
- psOutput: String; // Attribute data to write to file
- hFile: TextFile; // Handle to the output file
- iAttribCount: Integer; // Count of attributes associated with block
- iCount1: Integer; // Generic loop counter
- iCount2: Integer;
- begin
- // Set the cursor to an hourglass
- crCursor := Screen.Cursor;
- Screen.Cursor := crHourglass;
-
- // Get associated list of inserts from Vdraft
- psBlockName := lstBlocks.Items.Strings[lstBlocks.ItemIndex];
- vInserts := vDrawing.Blocks.Item( psBlockName ).Inserts;
-
- // Ask the user for a file name
- try
- cdOpen := TOpenDialog.Create( Application );
-
- // Set specific properties of the dialog
- cdOpen.Filter := 'All Files (*.*)|*.*|' +
- 'Attributes (*.att)|*.att|';
- cdOpen.Options := [ofPathMustExist, ofOverwritePrompt];
-
- // Did the user cancel?
- if cdOpen.Execute then begin
- try
- // Open the file for writing
- AssignFile( hFile, cdOpen.Filename );
- Rewrite( hFile );
-
- // Build up attribute list
- iAttribCount := vInserts.Item( 1 ).Attributes.Count;
- for iCount1 := 1 to vInserts.Count do begin
- vAttribs := vInserts.Item( iCount1 ).Attributes;
- psOutput := vAttribs.Item( 1 ).Text;
- for iCount2 := 2 to iAttribCount do
- psOutput := psOutput + ', ' + vAttribs.Item( iCount2 ).Text;
-
- // Write the complete attribute definition to the file
- WriteLn( hFile, psOutput );
- end;
-
- // Close the file when done
- CloseFile( hFile );
-
- except
- on Exception do
- MessageDlg( 'Unable to create ' + cdOpen.Filename, mtError, [mbOK], 0 );
- end;
- end;
-
- except
- on Exception do
- MessageDlg( 'Unable to initialize Windows Common Dialog...', mtError, [mbOK], 0 );
- end;
-
- // Free the dialog instance and restore starting cursor
- cdOpen.Free;
- Screen.Cursor := crCursor;
- end;
-
- { -----------------------------------------------------------------------------
- Method: lstAttributesClick( Sender: TObject )
-
- Parameters: Object or control that triggered the event
-
- Returns: Nothing
-
- Purpose: Event method triggered when the user has clicked the mouse
- pointer in the forms attribute list box.
- ----------------------------------------------------------------------------- }
- procedure TfrmAttributes.lstAttributesClick( Sender: TObject );
- begin
- // Enable the edit and zoom buttons
- btnEditAttributes.Enabled := True;
- btnZoom.Enabled := True;
- end;
-
- { -----------------------------------------------------------------------------
- Method: btnEditAttributesClick( Sender: TObject )
-
- Parameters: Object or control that triggered the event
-
- Returns: Nothing
-
- Purpose: Event method triggered when the user has clicked on the forms
- Edit Attributes button.
- ----------------------------------------------------------------------------- }
- procedure TfrmAttributes.btnEditAttributesClick( Sender: TObject );
- var
- vInserts: Variant; // Handle to specific block insertion
- psBlockName: String; // Name of specified block
- begin
- // Find the selected insert of the block name
- psBlockName := lstBlocks.Items.Strings[lstBlocks.ItemIndex];
- vInserts := vDrawing.Blocks.Item( psBlockName ).Inserts;
-
- // Display the properties for the selected insert
- vInserts.Item( lstAttributes.ItemIndex + 1 ).Dialog;
- end;
-
- { -----------------------------------------------------------------------------
- Method: btnZoomClick( Sender: TObject )
-
- Parameters: Object or control that triggered the event
-
- Returns: Nothing
-
- Purpose: Event method triggered when the user has clicked on the forms
- Zoom to Insert button.
- ----------------------------------------------------------------------------- }
- procedure TfrmAttributes.btnZoomClick( Sender: TObject );
- var
- vBlock: Variant; // Handle to specific block
- vInsert: Variant; // Handle to specific block insertion
- vOffset: Variant; // Handle to vector of block's basepoint
- vCorner1: Variant; // Handles to corners of block extents
- vCorner2: Variant;
- vCenter: Variant; // Handle to center of view
- vView: Variant; // Handle to active view
- psBlockName: String; // Name of specific block
- fWidth: Double; // Sizes of view
- fHeight: Double;
- begin
- // Find the selected block
- psBlockName := lstBlocks.Items.Strings[lstBlocks.ItemIndex];
- vBlock := vDrawing.Blocks.Item( psBlockName );
- vInsert := vBlock.Inserts.Item( lstAttributes.ItemIndex + 1 );
-
- // Start out with the extents of the block
- vCorner1 := vBlock.ExtentsMin;
- vCorner2 := vBlock.ExtentsMax;
-
- // Adjust the insert point by the block's basepoint
- // so we know how much to offset the block extents by
- vOffset := vBlock.Where;
- vOffset.Detach; // We want to change the vector but not the basepoint
- vOffset.SubtractAway( vInsert.Where );
- vCorner1.SubtractAway( vOffset );
- vCorner2.SubtractAway( vOffset );
-
- // F.find the center and height/width of insert area
- fWidth := vCorner2.x -vCorner1.x;
- fHeight := vCorner2.y - vCorner1.y;
- vCenter := vDraft.NewVector( vCorner1.x + fWidth / 2, vCorner1.y + fHeight / 2 );
-
- // Change the current view so it just shows the insert
- vView := vDrawing.Views.ActiveView;
- vView.Center( vCenter );
- vView.Width := 2 * fWidth;
- vView.Height := 2 * fHeight;
- end;
-
- { -----------------------------------------------------------------------------
- Method: btnQuitClick( Sender: TObject )
-
- Parameters: Object or control that triggered the event
-
- Returns: Nothing
-
- Purpose: Event method triggered when the user has clicked on the forms
- Quit button.
- ----------------------------------------------------------------------------- }
- procedure TfrmAttributes.btnQuitClick( Sender: TObject );
- begin
- // Close the application
- Close;
- end;
-
- end.
-
-