/* Graphik Import in DrawStudio mittels MetaView */

METAVIEW = ':Aminet/MetaView/MetaView'    /* Please complete the path */

OPTIONS RESULTS
SIGNAL ON FAILURE
SIGNAL ON SYNTAX

/*
** Looking for our start process
*/

DSPORT = ADDRESS()
if (LEFT(DSPORT, 10) ~= "DRAWSTUDIO") then do /* not started from DrawStudio */
    say "Please start me from DrawStudio!"
    EXIT
end
ADDRESS VALUE DSPORT
    PROJECT_LOCK

/*
** Create a new Metaview process:
** first look for the allready running processes
** then the new Port will be the next one
*/

do NUMBER = 0 to 20
    if (SHOW(PORTS,"METAVIEW." || NUMBER) = 0) then
    leave
end

/*
** Searching for MetaView: 1. our path above,
**    2. path in env:MetaView.path
** or 3. you must have a assign "MetaView:"
*/

if (EXISTS(METAVIEW)=0) then do
    if OPEN("MVVAR","env:MetaView.path","Read") then do
        METAVIEW = READLN("MVVAR")
    end
    if (EXISTS(METAVIEW)=0) then do
        METAVIEW = "MetaView:MetaView"
    end
end

/*
** Enable warnings for WaitForPort
*/

OPTIONS FAILAT 5
ADDRESS COMMAND
    "run " || METAVIEW || " NODISPLAY"
    MVPORT = "METAVIEW." || NUMBER
    "WaitForPort " || MVPORT

/*
** Ignore the other errors
*/

OPTIONS FAILAT 21

ADDRESS VALUE DSPORT
    REQ_FILE '"Please choose a file to import..."' '"ram:"' STEM req.
    if req.result == 0 then do
        EXIT
    end

/*
** Do all needed thinks with MetaView (LOAD,SAVE,QUIT...)
*/

ADDRESS VALUE MVPORT
    LOAD '"'req.file'"'
    SAVE "t:test.dr2d" AS DR2D
    QUIT

/*
**  Import the temporary file in application
*/

ADDRESS VALUE DSPORT
    PROJECT_PLACE "t:test.dr2d"
    REDRAW_OFF
    OBJECT_SELECT
    OBJECT_GROUP
    REDRAW_ON
    PROJECT_UNLOCK
    EXIT

FAILURE:
    ADDRESS COMMAND
    REQUESTCHOICE "Error" """Can't find" METAVIEW "!""" "OK"
    ADDRESS VALUE DSPORT
    REDRAW_ON
    PROJECT_UNLOCK
    EXIT

SYNTAX:
    say "Error on line" SIGL ":" ERRORTEXT(RC) "!"
    ADDRESS VALUE DSPORT
    REDRAW_ON
    PROJECT_UNLOCK
    EXIT
