DesignCAD 3D Max 2027 box
Now available

DesignCAD 2027

Hatches that follow their boundaries, an on-screen dial to turn your model, and a way to find any command by name, in a clean Windows 11 look.

Displaying text on Status Bar from VB

Started by Aggie, May 12, 2008, 11:56:28 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Aggie

I am using DesignCAD 17.2  as a drawing engine to carry out mapping and other graphical tasks driven by Excel and VBA. Some of these tasks are repetetive and take a long time to finish. I would like to display a progress indicator of some sort. The easiest way to do this should be via DesignCAD's status bar. The DesignCAD type library provides objects, properties and methods for checking whether the bar is on or off and for displaying various prompts, e.g. to set points by using something like
DcPts.Prompt = "Set a point for lower left corner of the histogram"
    DcPts.GetUserPoint

DrollTroll

Hi Aggie,

DesignCAD's status bar is pretty much tied to the use of the commands that are currently in operation. In a few specific cases you can change the prompt, but you can't set a sticky message that will remain in place while you run several DesignCAD commands -- each command will try to write its own message to the status bar as it runs.

You may have to create your own "progress" form and show it while the tasks are running.
25 years with DesignCAD

Rob S

#2

Edit -  I missed earlier noticing in the post above that you want to do this from your VB routine.

I do not know if you can use the sys(36) parameter from VB.  The following works in DCAD macros.

If you do not need status bar messages while your macro is running, the following macro has several progress indicators built into it.  You can break out and use whatever part you like.

1) It displays moving text on the drawing screen showing percent done.

2) It draws a line longer and longer, indicating progress.  Would be easy enough to add an endpoint.

3) It displays percent done, and a graphic progress line in the status bar.

Quote

for countxy = 1 to 10  'sets interval for progress could use step 10 to go from 10 to 100

test = 10 * countxy    'build text string for progress
testpre$ = test
if countxy < 10 then
Test$ = " " + left$(testpre$,2) + " % "
else
Test$ = left$(testpre$,3) + " % "
end if                  '

>Text   ' prints progress text on screen
{
<Text "Done "[test$]
<pointxyz [10, 2*countxy, 0]
}

>line     ' draws progress line on screen
{
<pointxyz [10, 2, 0]
<pointxyz [8 + 2 * countxy, 2, 0]
}


sys(36)=1   ' so updated status line will display

regen       ' only needed if you want the progress text and line in the drawing

print test$, STRING$(countxy, "="), STRING$(20-countxy*2, "-")   ' prints progress sting in

status line

sys(36)=2   ' prevents normal status bar text from overwriting progress bar

>Eraselast  ' erases screen line, remains visible due to silent mode
{
}

>Eraselast  ' erases screen text, remains visible due to silent mode
{
}


for count = 1 to 50  ' timeconsuming activity goes here

>Arc4
{
<Radius 0
<Pointxyz -10, 0, 0
<Pointxyz 0, 0, 0
<Pointxyz 10, 10, 0
}

>Eraselast
            {
            }

next count

next countxy
User since forever

Aggie

Thanks for that Rob. The DcDoc.SilentMode property is equivalent to SYS(36) so there's no problem there. I am already using it to speed up exectution. The graphic progress indicator in the status bar is exactly what I want but I can't find an equivalent to the PRINT statement accessible from VBA and Excel. Application.StatusBar = puts text onto Excel's status bar but it gets hidden behind the DesignCAD window unless I reduce the size of the DC window. Setting a sensible window size by means that work on PC's of unknown configuration is a trick in itself but this is what I am presently doing (see code below).

Your idea of actually drawing something on the DesignCad screen and then deleting it would work but would significantly slow processing if it happened every time the program looped. Excel, prior to 2007, was limited to 65536 rows so that is the only limit on the number of times the program might have to loop and I regularly process data sets with in excess of 50000 rows. It is also fraught with visibility problems if there are existing entities that the program knows nothing about. So that the actual mapping can be seen as it progresses, the program zooms the area to be mapped to fit the window without regard to existing entities. In most cases this is its own progress indicator except where the individual points are too small to be seen at the resulting scale and one is left looking at a white screen with nothing apparently happening or they blend into the colours of existing entities so that there is no obvious change occuring. I will think some more about this idea and the possibility of only updating every so often to avoid excessively slowing the program.

There are numerous posts describing how to use a custom dialog box/form as a progress indicator in Excel macros along the lines suggested by DrollTroll. These work fine as long as Excel isn't activating another application such as DesignCAD. They all suffer from the problem that the dialog box gets hidden by the DesignCAD window when it is active. I played with quite a few of these before deciding that the best option was to use the status bar. There are also lots of posts from people wanting to know how to make forms originating from Excel stay on top of other windows but I couldn't find any answers that work. The best I could come up with here was a form which I placed at the edge of the screen outside the DesignCAD window but it refused to update until after DesignCAD had finished. Any ideas here would help.

My original post got truncated somehow and posted prematurely. Here is an abridged version of the code I am presently using. It's a bit long but I have left all the bits required to access DesignCAD in for the benefit of anyone else who might want to run DesignCAD through Excel.
QuotePublic Declare Function GetSystemMetrics32 Lib "user32" _
    Alias "GetSystemMetrics" (ByVal nIndex As Long) As Long
Public Const SM_CXSCREEN = 0
Public Const SM_CYSCREEN = 1


Public Sub MyMap()
    'Declare variables to access and use DesignCAD
    Dim DcApp As DesignCAD.Application      'Application Class
    Dim DcDocs As DesignCAD.Documents       'Documents Class
    Dim DcDoc As DesignCAD.Document         'Document Class
    Dim DcPts As DesignCAD.CmdPoints        'CmdPoints Class
    Dim DcLine As DesignCAD.Line            'Line Class
    Dim DcLStyle As DesignCAD.LineStyle     'LineStyle Class
    Dim DcLayer As DesignCAD.Layer          'Layer Class
    Dim DcLayers As DesignCAD.Layers        'Layers Class
    Dim DcHatch As DesignCAD.Hatch          'Hatch Class
    Dim DcColour As DesignCAD.DcHatchTypes  'Hatch Types Class
    Dim DcText As DesignCAD.Text
    Dim DcTStyle As DesignCAD.TextStyle
    Dim DcOpt As DesignCAD.Option
    Dim DcSel As DesignCAD.Selection
    Dim DcEnts As DesignCAD.Entities
    Dim DcEnt As DesignCAD.Entity
    Dim DcUI As DesignCAD.UIObject          'User Interface object for accessing Status Bar

    'Declare Program data variables
    Dim DCSBar as Boolean                                           'Logical Variable to remember whether Status Bar was on or off
    Dim ThisPoint as Long, NumberOfPoints as Long
    Dim X() as Double, Y() as Double,                               'Variables to hold information for mapping
    Dim Red() As Integer, Green() As Integer, Blue() As Integer     'Colour components for mapping contours

    '===================================================================================
    ' Routines to collect information to be mapped go here
    '===================================================================================

    Set DcApp = DesignCAD.Application       ' Attach to a running instance of DesignCad or start a new one if there isn't one running
    DcApp.Visible = True
    DcApp.BringToTop
    DcApp.Top = 0                       'Move DesignCAD window to top left of screen
    DcApp.Left = 0
    DcApp.Width = GetSystemMetrics32(SM_CXSCREEN)   'Make DesignCAD window maximum screen width
    'Make DesignCAD window a bit less than max screen height to reveal Excel's status bar
    DcApp.Height = (GetSystemMetrics32(SM_CYSCREEN) * 0.9)

    Set DcDocs = DcApp.Documents            'Create a new drawing if there isn't one open
    If DcDocs.Count < 1 Then DcDocs.Add
    Set DcDoc = DcApp.ActiveDocument        'and attach to it
    DcDoc.DraftingMode = True               '2D drawing mode
    Set DcPts = DcDoc.CmdPoints

    Set DcOpt = DcDoc.Option                'Set up DC options the way I want them
    Set DcHatch = DcDoc.Hatch
    DcHatch.PatternName = "WIN_SOLID"       'Set hatch pattern to be used
    Set DcText = DcDoc.Text
    Set DcTStyle = DcDoc.TextStyle          ' Initialize text style settings for legend
    DcTStyle.Font = "Arial"
    DcTStyle.UseVectorFont = False
    DcTStyle.TextSize = Val(DCMapForm1.LegendSizeBox.Text)
    DcTStyle.FontStyle = dcFontRegular
    DcTStyle.TextJustify = dcJustifyLeft
       
    Set DcSel = DcDoc.Selection              'Set line options for drawing grid
    Set DcEnts = DcDoc.Entities
    Set DcLine = DcDoc.Line
    Set DcLStyle = DcDoc.LineStyle
    DcLStyle.FilledLine = True
    DcLStyle.LineType = dcLineContinuous
       
    DcSel.DeSelectAll                       'Clear any existing selection
    DcDoc.SilentMode = True                 'Turn on silent mode for faster mapping
    DcDoc.PointSelectMode = False
       
    Set DcUI = DcApp.UIObject
    DCSBar = DcUI.StatusBarOn               'Save Status Bar setting for restoration later
    DcUI.StatusBarOn = True                 'Make sure DC status bar is on
   
    For ThisPoint = 1 to NumberOfPoints
        DcDoc.CurrentColor = RGB(Red(ThisPoint), Green(ThisPoint), Blue(ThisPoint))   'Set colour for point
        'Set vertices for rectangle to hatch
        DcPts.SetCommandPoint (X(ThisPoint) - 2.5), (Y(ThisPoint) - 2.5), 0
        DcPts.SetCommandPoint (X(ThisPoint) + 2.5), (Y(ThisPoint) - 2.5), 0
        DcPts.SetCommandPoint (X(ThisPoint) + 2.5), (Y(ThisPoint) + 2.5), 0
        DcPts.SetCommandPoint (X(ThisPoint) - 2.5), (Y(ThisPoint) + 2.5), 0
        DcPts.SetCommandPoint (X(ThisPoint) - 2.5), (Y(ThisPoint) - 2.5), 0
        DcHatch.Draw        'Draw the hatch.
        'Put a progress message in the Excel Status Bar
        Application.StatusBar = Format((NPoint / NumberOfPoints * 100), "0") & "% points processed"
    Next

    DcDoc.SilentMode = False                 'Turn on silent mode back off for normal operation after exit
    DcUI.StatusBarOn = DCSBar               'Restore original DesignCAD Status Bar setting
    Application.StatusBar = False       'Clear Excel Status bar

    '===================================================================================
    ' Wrap it up and exit
End Sub

Can anyone come up with code to replace the last line before the Next (the one starting Application.StatusBar =) so that the text appears on DesignCAD's status bar instead of on Excel's bar?

prl

#4
It is unfortunate that OLE is so very slow when it constantly has to communicate with the DesignCAD server as occurs when drawing things.  A coding strategy you may want to consider rather than trying to put up a progress bar is to have your OLE output the data to a text file and have the OLE run an embedded basiccad program to draw the data.  Basiccad is lighting fast when compared to OLE, probably twenty times faster.  The embedded basiccad program is straight basiccad code stored in an invisible text memo component in your OLE that you "Save to File" and then "Run" from OLE.

Aggie

Hi prl,
I was not aware that you could embed and run Basiccad code from OLE in the manner you suggest. Can you give me a link to an example or other documentation. I have been trying to find it without any success.
Your speed difference got me interested because I originally wrote the mapping routine in Basiccad. The Basiccad version read the point data from a .csv file saved by Excel. I ran the Basiccad routine from the Tools| Macro Execute menu and supplied the location of the data file and the data range information manually. I switched to VBA mainly to avoid the need to create the temporary .csv file and automate the transfer of the data range information. In the process, I added other functionality so, overall, the job was completed more quickly using VBA. I had not benchmarked the speed of the plotting routines although my gut feeling was that VBA was faster. I benchmarked them this morning on the attached sample map of crop yield at 51,433 points using an eight-band colour scale. I timed from the plotting of the first point to the last point. In Basiccad, plotting took 5 m 24.59 s and in VBA it took 5 m 18.18 s. The VBA routine was showing progress in the Excel status bar while running. There was no progress indication in the Basiccad routine. This magnitude of speed difference is negligible. I actually expected a larger difference but that might be because when I ran the Basiccad program in the past, its data file was usually on a server whereas for today's test I put the temporary file on the local hard drive. Is the Basiccad code likely to run faster from OLE?

prl

Aggie, not sure if I mispoke regarding the drawing speed comparison or you have some code in the basiccad program that is significantly slowing things down.  My experience is that anything that passes through an olevarient is slow.  But then again I don't plot data but rather constantly poll the DesignCAD server for the status of about two to three hundred layers. This just doesn't work if I use OLE straight, I use the embedded basic routine for this.

I'm out of town at the moment, should be back after Memorial Day so I don't have access to the programming tools.  To do the embedded basiccad, try this sample OLE application.  Create a new form with one text memo component and an "ok" button.  In the text memo, cut and past your basiccad program.   For the "ok" button, create some event code that saves the contents of the text memo component to file, say "C:\temp.d3m".  Have it wait 200 ms and then execute the DcDoc1.RunBasicCad("c:\temp.d3m").  I'm just guessing at the method syntax but it should be something like that. That's about all there is to it. You can hide the text memo component and clean up (destroy) the temp.d3m after it finishes but those are extra things you can code if you want.

DrollTroll

#7
BasicCAD itself won't run any faster when run from Excel -- it's still running directly in DesignCAD's application space. However, BasicCAD in SILENT MODE (Sys(36) = 2) runs much faster than BasicCAD in normal mode if points are being set or objects being drawn.

You could also speed up your Automation code by presetting the points for each hatch box in an array of doubles, then passing the entire array SetCmdPoints(5, ptArray). It may seem a little tricky setting up the array the first time you do it (it's one-dimensional, length = 3* number of points, and points are in x,y,z,x,y,z,x,y,z.... order). But it's generally much faster to pass several points at once via SetCmdPoints than to pass them one at a time using SetCmdPoint.

Even if we had a direct means of setting the text in the status bar, use of Silent Mode would probably preclude its viability as a progress indicator -- Silent Mode gains its efficiency by not updating the UI components.
25 years with DesignCAD

Aggie

Hi Droll Troll,
Thanks for the Setcmdpoints tip. That cut the plotting time for the sample I posted earlier from 5 m 18.18 s down to 4 m 1.77 s which is a useful improvement when one is sitting waiting for it to finish.
Incidentally, I have not been able to find any reference to Setcmdpoints in any of the documentation that I have. All the examples in the developers toolkit use Setcmdpoint multiple times.
Somewhere in the documentation, I came across a reference to a Programmer's Reference Manual but I can not find this document. Is it available for recent versions or is it an historical artifact?

Rob S


Watch out for V18 bug, it won't import unless a Z co-ordinate is included in the file. 

Make then all 0 if you want 2D imports.
User since forever

bdeck

#10
Hello all,
 
I've had good luck writing ascii files from XL/VBA  and then loading the entire file into DC in a single operation.

Aggie, if OLE is the bottleneck, then might this method provide a faster solution in your app?

I typically write to dc2 files from VBA,  and then use DC to convert the files to DXF for my CNC vendor.  Discussed briefly at http://forum.designcad.com/index.php?topic=754.0

Attached is a sample DC2 file with a winsolid hatch, which can be examined for file format.


For simple polylines, XYZ files are even simpler to write and import. (x,y,cr,x,y,cr...  , extra cr to start new polyline).
The ImportXYZ macro command is not well documented. I presume it accepts  <point[] parameters as in the Merge command.

>ImportXYZ
{
<Filename "*\XYZFiles\Test1.XYZ"
}


BD


PS Thanks, Rob, for the prescient heads up on xyz files in V 18.  Maybe this will be fixed in 18.3.

bdeck

#11
Aggie,

After a small timing test, it appears that your speed problem (in BasicCad at least) is due to the time it takes DC to execute multiple pointxyz commands within multiple hatch commands, even in silent mode.

While that process can take minutes, either Basiccad or VBA can write 10,000 hatch records to a dc2 file in a less than a second, and DC can load and display that file in a couple of seconds.

You dont have to erase the temporary file. Just reuse the same name and write over it.

BD





prl

#12
Quote from: bdeck on May 26, 2008, 01:12:32 PM
While that process can take minutes, either Basiccad or VBA can write 10,000 hatch records to a dc2 file in a less than a second, and DC can load and display that file in a couple of seconds.

Hi Bd, looks like you and I get interested in similar things. Your approach to writing out to a dc2 file and then loading/merging is very creative. Nice tip to store away for some future need.
 

bdeck


Quote from: prl on May 26, 2008, 02:01:02 PM
Hi Bd, looks like you and I get interested in similar things.


Hi prl,

It's a curse.

;)

Aggie

Hi all,

I have made progress since my last post. I was showing my program to a colleague who noticed the colour patch for the color toolbox button in the main toolbox flickering as the colors being drawn changed. Behold, turning off the main toolbox while drawing cut about 30 s off the job even in silent mode. It's actually quite difficult to get repeatable speed figures because of all the background tasks XP has running even with nothing but Excel and DC in the task bar. Thanks to your combined suggestions, the time required to plot a dataset is now about half what it was when I posted my question.

To respond to bdeck's suggestion of writing a dc2 file directly from VBA. I agree with your analysis of the bottleneck and  had thought that writing a dc2 file would be quicker. In my line of work, useful information can sometimes be gained by watching the points plot provided it doesn't take too long. As an example, mapping the data logged from the yield monitor on a grain harvester shows the sequence in which the field was harvested and where the harvester(s) actually drove. Loading a dc2 file written by VBA would be too fast to observe the sequence. (You'd be amazed by some of the things contractors do when they think nobody is watching.) This can sometimes explain all sorts of anomalies and stuff ups in the data. On balance, I think I'll stick to letting DC do the plotting most of the time although I might try writing the dc2 file for mapping high resolution airborne imagery and the like where sequence doeasn't matter.

prl,
Although the maps show the data at 51433 points there are typically only about 11000 entities in each map because, to save plotting time, I have plotted adjacent points in each east-west strip and within the same contour interval as a single rectangular hatch. That 11000 can vary enormously depending on the scale at which the parameter being mapped varies. I have zipped up and attached a sample file which contains several layers. One of the layers contains the background stuff including the grid, scale bar and North arrow. Most of the other layers contain contour maps of varying complexity. Feel free to play with it but, if you want to include it in any publication, please ask first because there are copyright issues attached to the data depicted.

I have the sample programs from the V13 SDK. I initially searched for "SetCmdPoints" which I copied and pasted from DrollTroll's post. The reason I didn't find any references is because this method doesn't exist. The autocomplete while I was coding showed me that the correct syntax is "SetCommandPoints" Once I got that right, I found three examples using it including SPEEDFRM.FRM although it took a while for the penny to drop.