Search in sources :

Example 1 with RootModel

use of artisynth.core.workspace.RootModel in project artisynth_core by artisynth.

the class Main method createRootModel.

protected RootModel createRootModel(Class<?> demoClass, String modelName, String[] args) {
    // reset in case previous model set this to false:
    ModelComponentBase.enforceUniqueNames = true;
    if (args == null) {
        args = new String[0];
    }
    try {
        RootModel newRoot = null;
        Method method = demoClass.getMethod("build", String[].class);
        if (demoClass == RootModel.class || method.getDeclaringClass() != RootModel.class) {
            // System.out.println (
            // "constructing model with build method ...");
            Constructor<?> constructor = demoClass.getConstructor();
            newRoot = (RootModel) constructor.newInstance();
            newRoot.setName(modelName);
            newRoot.setMainViewer(myViewer);
            newRoot.build(args);
        } else {
            System.out.println("constructing model with legacy constructor method ...");
            Constructor<?> constructor = demoClass.getConstructor(String.class);
            newRoot = (RootModel) constructor.newInstance(modelName);
        }
        return newRoot;
    } catch (Exception e) {
        myErrMsg = " class " + demoClass.getName() + " cannot be instantiated";
        if (e.getMessage() != null) {
            myErrMsg += ": \n" + e.getMessage();
        }
        e.printStackTrace();
        return null;
    }
}
Also used : RootModel(artisynth.core.workspace.RootModel) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) InternalErrorException(maspack.util.InternalErrorException)

Example 2 with RootModel

use of artisynth.core.workspace.RootModel in project artisynth_core by artisynth.

the class Main method setMaxStep.

public void setMaxStep(double sec) {
    if (myMaxStep != sec) {
        doSetMaxStep(sec);
        RootModel root = getRootModel();
        if (root != null) {
            root.setMaxStepSize(myMaxStep);
        }
    }
}
Also used : RootModel(artisynth.core.workspace.RootModel)

Example 3 with RootModel

use of artisynth.core.workspace.RootModel in project artisynth_core by artisynth.

the class Main method loadModelFile.

public boolean loadModelFile(File file) throws IOException {
    // the Jython console.
    if (myViewer != null && !SwingUtilities.isEventDispatchThread()) {
        LoadModelFileRunner runner = new LoadModelFileRunner(file);
        if (!runInSwing(runner)) {
            return false;
        } else {
            return runner.getStatus();
        }
    }
    RootModel newRoot = null;
    clearRootModel();
    ReaderTokenizer rtok = ArtisynthIO.newReaderTokenizer(file);
    if (rtok.nextToken() == ReaderTokenizer.TT_WORD) {
        try {
            newRoot = (RootModel) ClassAliases.newInstance(rtok.sval, RootModel.class);
            myModelFile = file;
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (newRoot == null) {
            throw new IOException("cannot create instance of " + rtok.sval);
        }
    } else {
        rtok.pushBack();
        newRoot = new RootModel();
    }
    // getWorkspace().getWayPoints().clear();
    long t0 = System.nanoTime();
    ScanWriteUtils.scanfull(rtok, newRoot, newRoot);
    long t1 = System.nanoTime();
    System.out.println("File scan time: " + ((t1 - t0) * 1e-9) + " sec");
    System.out.println("File size: " + file.length());
    // System.out.println ("queue size=" + ModelComponentBase.scanQueueSize());
    rtok.close();
    String modelName = newRoot.getName();
    if (modelName == null) {
        // use file name with extension stripped off
        modelName = file.getName();
        int dotIdx = modelName.indexOf('.');
        if (dotIdx != -1) {
            modelName = modelName.substring(0, dotIdx);
        }
    }
    setRootModel(newRoot, modelName, null);
    return true;
}
Also used : RootModel(artisynth.core.workspace.RootModel) ReaderTokenizer(maspack.util.ReaderTokenizer) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) InternalErrorException(maspack.util.InternalErrorException) WayPoint(artisynth.core.probes.WayPoint)

Example 4 with RootModel

use of artisynth.core.workspace.RootModel in project artisynth_core by artisynth.

the class Main method componentChanged.

public void componentChanged(ComponentChangeEvent e) {
    if (e.getCode() == ComponentChangeEvent.Code.STRUCTURE_CHANGED || e.getCode() == ComponentChangeEvent.Code.DYNAMIC_ACTIVITY_CHANGED) {
        boolean invalidateWaypoints = false;
        RootModel root = getRootModel();
        ModelComponent c = e.getComponent();
        if (myFrame != null && c != null && (c == root || c.getParent() == root)) {
            updateApplicationMenuPresent(root);
        }
        if (root != null) {
            invalidateWaypoints = changeAffectsWaypoints(root, e);
            if (invalidateWaypoints) {
                root.getWayPoints().invalidateAfterTime(0);
            }
        }
        if (myTimeline != null) {
            if (root != null && c == root.getWayPoints()) {
                myTimeline.requestUpdateWidgets();
            } else {
                // update timeline display regardless
                myTimeline.requestUpdateDisplay();
            }
        }
        if (e.getCode() == ComponentChangeEvent.Code.STRUCTURE_CHANGED) {
            if (c != null && c instanceof CompositeComponent && myFrame != null) {
                myFrame.getNavPanel().updateStructure(c);
                if (root != null && (c == root.getInputProbes() || c == root.getOutputProbes())) {
                    myTimeline.updateProbes(root);
                }
            }
            if (!getScheduler().isPlaying()) {
                rerender();
            }
        }
        // if (invalidateWaypoints)  && !myScheduler.isPlaying()) {
        if (invalidateWaypoints) {
            myScheduler.invalidateInitialState();
        }
    } else if (e.getCode() == ComponentChangeEvent.Code.NAME_CHANGED) {
        ModelComponent c = e.getComponent();
        if (myFrame != null) {
            if (c.getParent() != null) {
                myFrame.getNavPanel().updateStructure(c.getParent());
            } else {
                myFrame.getNavPanel().updateStructure(c);
            }
        }
    } else if (e.getCode() == ComponentChangeEvent.Code.PROPERTY_CHANGED) {
        PropertyChangeEvent pe = (PropertyChangeEvent) e;
        ModelComponent c = e.getComponent();
        if (c == getRootModel()) {
            if (pe.getPropertyName().equals("maxStepSize")) {
                doSetMaxStep(getRootModel().getMaxStepSize());
            } else if (pe.getPropertyName().equals("defaultViewOrientation")) {
                if (myViewerManager != null) {
                    myViewerManager.resetViewers(getDefaultViewOrientation(getRootModel()));
                }
            }
        } else if (pe.getPropertyName().startsWith("navpanel")) {
            if (myFrame != null) {
                myFrame.getNavPanel().updateStructure(e.getComponent());
            }
        }
    } else if (e.getCode() == ComponentChangeEvent.Code.GEOMETRY_CHANGED) {
        // myTimeline.requestUpdateDisplay();
        if (!getScheduler().isPlaying()) {
            rerender();
        }
    }
}
Also used : PropertyChangeEvent(artisynth.core.modelbase.PropertyChangeEvent) RootModel(artisynth.core.workspace.RootModel) ModelComponent(artisynth.core.modelbase.ModelComponent) CompositeComponent(artisynth.core.modelbase.CompositeComponent)

Example 5 with RootModel

use of artisynth.core.workspace.RootModel in project artisynth_core by artisynth.

the class Main method saveModelFile.

public void saveModelFile(File file, String fmtStr) throws IOException {
    RootModel root = getRootModel();
    if (root != null) {
        IndentingPrintWriter pw = ArtisynthIO.newIndentingPrintWriter(file);
        if (!root.getClass().isAssignableFrom(RootModel.class)) {
            pw.println(ClassAliases.getAliasOrName(root.getClass()));
        }
        root.write(pw, new NumberFormat(fmtStr), root);
        pw.close();
    }
}
Also used : RootModel(artisynth.core.workspace.RootModel) IndentingPrintWriter(maspack.util.IndentingPrintWriter) NumberFormat(maspack.util.NumberFormat)

Aggregations

RootModel (artisynth.core.workspace.RootModel)44 ModelComponent (artisynth.core.modelbase.ModelComponent)10 InternalErrorException (maspack.util.InternalErrorException)7 IOException (java.io.IOException)6 WayPoint (artisynth.core.probes.WayPoint)4 FileNotFoundException (java.io.FileNotFoundException)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 Probe (artisynth.core.probes.Probe)3 WayPointProbe (artisynth.core.probes.WayPointProbe)3 File (java.io.File)3 Property (maspack.properties.Property)3 ControlPanel (artisynth.core.gui.ControlPanel)2 CompositeComponent (artisynth.core.modelbase.CompositeComponent)2 TracingProbe (artisynth.core.probes.TracingProbe)2 Point (java.awt.Point)2 JFileChooser (javax.swing.JFileChooser)2 JMenuItem (javax.swing.JMenuItem)2 JSeparator (javax.swing.JSeparator)2 CompositeProperty (maspack.properties.CompositeProperty)2 ArgParser (argparser.ArgParser)1