Search in sources :

Example 1 with DensityCollection

use of org.bimserver.models.store.DensityCollection in project BIMserver by opensourceBIM.

the class CheckinDatabaseAction method execute.

@Override
public ConcreteRevision execute() throws UserException, BimserverDatabaseException {
    try {
        if (fileSize == -1) {
            setProgress("Deserializing IFC file...", -1);
        } else {
            setProgress("Deserializing IFC file...", 0);
        }
        if (getModel().size() == 0) {
            throw new DeserializeException(DeserializerErrorCode.EMPTY_MODEL_CANNOT_BE_CHECKED_IN, "Cannot checkin empty model");
        }
        authorization.canCheckin(poid);
        project = getProjectByPoid(poid);
        int nrConcreteRevisionsBefore = project.getConcreteRevisions().size();
        User user = getUserByUoid(authorization.getUoid());
        if (project == null) {
            throw new UserException("Project with poid " + poid + " not found");
        }
        if (!authorization.hasRightsOnProjectOrSuperProjects(user, project)) {
            throw new UserException("User has no rights to checkin models to this project");
        }
        if (!MailSystem.isValidEmailAddress(user.getUsername())) {
            throw new UserException("Users must have a valid e-mail address to checkin");
        }
        if (getModel() != null) {
            checkCheckSum(project, getModel());
        }
        long size = 0;
        if (getModel() != null) {
            for (IdEObject idEObject : getModel().getValues()) {
                if (idEObject.eClass().getEAnnotation("hidden") == null) {
                    size++;
                }
            }
            getModel().fixInverseMismatches();
        }
        for (ModelCheckerInstance modelCheckerInstance : project.getModelCheckers()) {
            if (modelCheckerInstance.isValid()) {
                ModelCheckerPlugin modelCheckerPlugin = getBimServer().getPluginManager().getModelCheckerPlugin(modelCheckerInstance.getModelCheckerPluginClassName(), true);
                if (modelCheckerPlugin != null) {
                    ModelChecker modelChecker = modelCheckerPlugin.createModelChecker(null);
                    ModelCheckerResult result = modelChecker.check(getModel(), modelCheckerInstance.getCompiled());
                    if (!result.isValid()) {
                        throw new UserException("Model is not valid according to " + modelCheckerInstance.getName());
                    }
                }
            }
        }
        CreateRevisionResult result = createNewConcreteRevision(getDatabaseSession(), size, project, user, comment.trim());
        concreteRevision = result.getConcreteRevision();
        IfcHeader ifcHeader = getModel().getModelMetaData().getIfcHeader();
        if (ifcHeader != null) {
            getDatabaseSession().store(ifcHeader);
            concreteRevision.setIfcHeader(ifcHeader);
        }
        project.getConcreteRevisions().add(concreteRevision);
        if (getModel() != null) {
            concreteRevision.setChecksum(getModel().getModelMetaData().getChecksum());
        }
        final NewRevisionAdded newRevisionAdded = getDatabaseSession().create(NewRevisionAdded.class);
        newRevisionAdded.setDate(new Date());
        newRevisionAdded.setExecutor(user);
        final Revision revision = concreteRevision.getRevisions().get(0);
        if (newServiceId != -1) {
            NewService newService = getDatabaseSession().get(newServiceId, OldQuery.getDefault());
            revision.getServicesLinked().add(newService);
        }
        concreteRevision.setSummary(new SummaryMap(getModel()).toRevisionSummary(getDatabaseSession()));
        // If this revision is being created by an external service, store a link to the service in the revision
        if (authorization instanceof ExplicitRightsAuthorization) {
            ExplicitRightsAuthorization explicitRightsAuthorization = (ExplicitRightsAuthorization) authorization;
            if (explicitRightsAuthorization.getSoid() != -1) {
                Service service = getDatabaseSession().get(explicitRightsAuthorization.getSoid(), OldQuery.getDefault());
                revision.setService(service);
            }
        }
        newRevisionAdded.setRevision(revision);
        newRevisionAdded.setProject(project);
        newRevisionAdded.setAccessMethod(getAccessMethod());
        Revision lastRevision = project.getLastRevision();
        IfcModelInterface ifcModel = null;
        if (merge && lastRevision != null) {
            ifcModel = checkinMerge(lastRevision);
        } else {
            ifcModel = getModel();
        }
        ifcModel.fixOidsFlat(getDatabaseSession());
        if (getBimServer().getServerSettingsCache().getServerSettings().isGenerateGeometryOnCheckin()) {
            setProgress("Generating Geometry...", -1);
            UserSettings userSettings = user.getUserSettings();
            RenderEnginePluginConfiguration defaultRenderEngine = userSettings.getDefaultRenderEngine();
            if (defaultRenderEngine == null) {
                throw new UserException("No default render engine has been selected for this user");
            }
            RenderEnginePool pool = getBimServer().getRenderEnginePools().getRenderEnginePool(model.getPackageMetaData().getSchema(), defaultRenderEngine.getPluginDescriptor().getPluginClassName(), getBimServer().getPluginSettingsCache().getPluginSettings(defaultRenderEngine.getOid()));
            GenerateGeometryResult generateGeometry = new GeometryGenerator(getBimServer()).generateGeometry(pool, getBimServer().getPluginManager(), getDatabaseSession(), ifcModel, project.getId(), concreteRevision.getId(), true, geometryCache);
            // TODO OUTDATED!!!
            // TODO deduplicate (this is duplicated 4 times now!)
            concreteRevision.setMultiplierToMm(generateGeometry.getMultiplierToMm());
            concreteRevision.setBounds(generateGeometry.getBounds());
            concreteRevision.setBoundsUntransformed(generateGeometry.getBoundsUntransformed());
            DensityCollection densityCollection = getDatabaseSession().create(DensityCollection.class);
            concreteRevision.eSet(StorePackage.eINSTANCE.getConcreteRevision_DensityCollection(), densityCollection);
            Collections.sort(generateGeometry.getDensities(), new Comparator<Density>() {

                @Override
                public int compare(Density o1, Density o2) {
                    return (int) ((o1.getDensityValue() - o2.getDensityValue()) * 10000000);
                }
            });
            for (Density density : generateGeometry.getDensities()) {
                org.bimserver.models.store.Density dbDensity = getDatabaseSession().create(org.bimserver.models.store.Density.class);
                dbDensity.setDensity(density.getDensityValue());
                dbDensity.setGeometryInfoId(density.getGeometryInfoId());
                dbDensity.setTrianglesBelow(density.getNrPrimitives());
                dbDensity.setVolume(density.getVolume());
                densityCollection.getDensities().add(dbDensity);
            }
            for (Revision other : concreteRevision.getRevisions()) {
                other.setHasGeometry(true);
            }
        }
        if (nrConcreteRevisionsBefore != 0 && !merge) {
            // There already was a revision, lets delete it (only when not merging)
            concreteRevision.setClear(true);
        }
        Set<EClass> eClasses = ifcModel.getUsedClasses();
        Map<String, Long> startOids = getDatabaseSession().getStartOids();
        if (startOids == null) {
            throw new BimserverDatabaseException("No objects changed");
        }
        OidCounters oidCounters = new OidCounters();
        for (EClass eClass : eClasses) {
            long oid = startOids.get(eClass);
            if (!DatabaseSession.perRecordVersioning(eClass)) {
                oidCounters.put(eClass, oid);
            }
        }
        concreteRevision.setOidCounters(oidCounters.getBytes());
        if (ifcModel != null) {
            getDatabaseSession().store(ifcModel.getValues(), project.getId(), concreteRevision.getId());
        }
        getDatabaseSession().addPostCommitAction(new PostCommitAction() {

            @Override
            public void execute() throws UserException {
                try {
                    clearCheckinInProgress();
                } catch (BimserverDatabaseException | ServiceException e) {
                    LOGGER.error("", e);
                }
                getBimServer().getNotificationsManager().notify(new NewRevisionNotification(getBimServer(), project.getOid(), revision.getOid(), authorization));
            }
        });
        getDatabaseSession().store(concreteRevision);
        getDatabaseSession().store(project);
    } catch (Throwable e) {
        try {
            clearCheckinInProgress();
        } catch (ServiceException e1) {
            LOGGER.error("", e1);
        }
        if (e instanceof BimserverDatabaseException) {
            throw (BimserverDatabaseException) e;
        }
        if (e instanceof UserException) {
            throw (UserException) e;
        }
        LOGGER.error("", e);
        throw new UserException(e);
    }
    return concreteRevision;
}
Also used : User(org.bimserver.models.store.User) RenderEnginePool(org.bimserver.renderengine.RenderEnginePool) IfcModelInterface(org.bimserver.emf.IfcModelInterface) Density(org.bimserver.geometry.Density) ModelCheckerResult(org.bimserver.models.store.ModelCheckerResult) BimserverDatabaseException(org.bimserver.BimserverDatabaseException) ModelChecker(org.bimserver.plugins.modelchecker.ModelChecker) EClass(org.eclipse.emf.ecore.EClass) ExplicitRightsAuthorization(org.bimserver.webservices.authorization.ExplicitRightsAuthorization) OidCounters(org.bimserver.database.OidCounters) UserException(org.bimserver.shared.exceptions.UserException) NewRevisionAdded(org.bimserver.models.log.NewRevisionAdded) NewService(org.bimserver.models.store.NewService) ModelCheckerInstance(org.bimserver.models.store.ModelCheckerInstance) ModelCheckerPlugin(org.bimserver.plugins.modelchecker.ModelCheckerPlugin) IdEObject(org.bimserver.emf.IdEObject) UserSettings(org.bimserver.models.store.UserSettings) PostCommitAction(org.bimserver.database.PostCommitAction) GeometryGenerator(org.bimserver.GeometryGenerator) Service(org.bimserver.models.store.Service) NewService(org.bimserver.models.store.NewService) DeserializeException(org.bimserver.plugins.deserializers.DeserializeException) IfcHeader(org.bimserver.models.store.IfcHeader) Date(java.util.Date) RenderEnginePluginConfiguration(org.bimserver.models.store.RenderEnginePluginConfiguration) NewRevisionNotification(org.bimserver.notifications.NewRevisionNotification) Revision(org.bimserver.models.store.Revision) ConcreteRevision(org.bimserver.models.store.ConcreteRevision) SummaryMap(org.bimserver.SummaryMap) ServiceException(org.bimserver.shared.exceptions.ServiceException) GenerateGeometryResult(org.bimserver.GenerateGeometryResult) DensityCollection(org.bimserver.models.store.DensityCollection)

Example 2 with DensityCollection

use of org.bimserver.models.store.DensityCollection in project BIMserver by opensourceBIM.

the class GenericCheckinDatabaseAction method generateDensityAndBounds.

protected void generateDensityAndBounds(CreateRevisionResult result, GenerateGeometryResult generateGeometry, ConcreteRevision concreteRevision) throws BimserverDatabaseException {
    for (Revision revision : result.getRevisions()) {
        Bounds newBounds = GeometryFactory.eINSTANCE.createBounds();
        Vector3f min = GeometryFactory.eINSTANCE.createVector3f();
        min.setX(Double.MAX_VALUE);
        min.setY(Double.MAX_VALUE);
        min.setZ(Double.MAX_VALUE);
        Vector3f max = GeometryFactory.eINSTANCE.createVector3f();
        max.setX(-Double.MAX_VALUE);
        max.setY(-Double.MAX_VALUE);
        max.setZ(-Double.MAX_VALUE);
        newBounds.setMin(min);
        newBounds.setMax(max);
        Bounds newBoundsMm = GeometryFactory.eINSTANCE.createBounds();
        Vector3f minMm = GeometryFactory.eINSTANCE.createVector3f();
        minMm.setX(Double.MAX_VALUE);
        minMm.setY(Double.MAX_VALUE);
        minMm.setZ(Double.MAX_VALUE);
        Vector3f maxMm = GeometryFactory.eINSTANCE.createVector3f();
        maxMm.setX(-Double.MAX_VALUE);
        maxMm.setY(-Double.MAX_VALUE);
        maxMm.setZ(-Double.MAX_VALUE);
        newBoundsMm.setMin(minMm);
        newBoundsMm.setMax(maxMm);
        Bounds newBoundsu = GeometryFactory.eINSTANCE.createBounds();
        Vector3f minu = GeometryFactory.eINSTANCE.createVector3f();
        minu.setX(Double.MAX_VALUE);
        minu.setY(Double.MAX_VALUE);
        minu.setZ(Double.MAX_VALUE);
        Vector3f maxu = GeometryFactory.eINSTANCE.createVector3f();
        maxu.setX(-Double.MAX_VALUE);
        maxu.setY(-Double.MAX_VALUE);
        maxu.setZ(-Double.MAX_VALUE);
        newBoundsu.setMin(minu);
        newBoundsu.setMax(maxu);
        Bounds newBoundsuMm = GeometryFactory.eINSTANCE.createBounds();
        Vector3f minuMm = GeometryFactory.eINSTANCE.createVector3f();
        minuMm.setX(Double.MAX_VALUE);
        minuMm.setY(Double.MAX_VALUE);
        minuMm.setZ(Double.MAX_VALUE);
        Vector3f maxuMm = GeometryFactory.eINSTANCE.createVector3f();
        maxuMm.setX(-Double.MAX_VALUE);
        maxuMm.setY(-Double.MAX_VALUE);
        maxuMm.setZ(-Double.MAX_VALUE);
        newBoundsuMm.setMin(minuMm);
        newBoundsuMm.setMax(maxuMm);
        revision.setBounds(newBounds);
        revision.setBoundsUntransformed(newBoundsu);
        revision.setBoundsMm(newBoundsMm);
        revision.setBoundsUntransformedMm(newBoundsuMm);
        for (ConcreteRevision concreteRevision2 : revision.getConcreteRevisions()) {
            Vector3f min2 = concreteRevision2.getBounds().getMin();
            Vector3f max2 = concreteRevision2.getBounds().getMax();
            float mm = concreteRevision2.getMultiplierToMm();
            if (min2.getX() < min.getX()) {
                min.setX(min2.getX());
            }
            if (min2.getY() < min.getY()) {
                min.setY(min2.getY());
            }
            if (min2.getZ() < min.getZ()) {
                min.setZ(min2.getZ());
            }
            if (max2.getX() > max.getX()) {
                max.setX(max2.getX());
            }
            if (max2.getY() > max.getY()) {
                max.setY(max2.getY());
            }
            if (max2.getZ() > max.getZ()) {
                max.setZ(max2.getZ());
            }
            if (min2.getX() * mm < minMm.getX()) {
                minMm.setX(min2.getX() * mm);
            }
            if (min2.getY() * mm < minMm.getY()) {
                minMm.setY(min2.getY() * mm);
            }
            if (min2.getZ() * mm < minMm.getZ()) {
                minMm.setZ(min2.getZ() * mm);
            }
            if (max2.getX() * mm > maxMm.getX()) {
                maxMm.setX(max2.getX() * mm);
            }
            if (max2.getY() * mm > maxMm.getY()) {
                maxMm.setY(max2.getY() * mm);
            }
            if (max2.getZ() * mm > maxMm.getZ()) {
                maxMm.setZ(max2.getZ() * mm);
            }
            Vector3f min2u = concreteRevision2.getBoundsUntransformed().getMin();
            Vector3f max2u = concreteRevision2.getBoundsUntransformed().getMax();
            if (min2u.getX() < minu.getX()) {
                minu.setX(min2u.getX());
            }
            if (min2u.getY() < minu.getY()) {
                minu.setY(min2u.getY());
            }
            if (min2u.getZ() < minu.getZ()) {
                minu.setZ(min2u.getZ());
            }
            if (max2u.getX() > maxu.getX()) {
                maxu.setX(max2u.getX());
            }
            if (max2u.getY() > maxu.getY()) {
                maxu.setY(max2u.getY());
            }
            if (max2u.getZ() > maxu.getZ()) {
                maxu.setZ(max2u.getZ());
            }
            if (min2u.getX() * mm < minuMm.getX()) {
                minuMm.setX(min2u.getX() * mm);
            }
            if (min2u.getY() * mm < minuMm.getY()) {
                minuMm.setY(min2u.getY() * mm);
            }
            if (min2u.getZ() * mm < minuMm.getZ()) {
                minuMm.setZ(min2u.getZ() * mm);
            }
            if (max2u.getX() * mm > maxuMm.getX()) {
                maxuMm.setX(max2u.getX() * mm);
            }
            if (max2u.getY() * mm > maxuMm.getY()) {
                maxuMm.setY(max2u.getY() * mm);
            }
            if (max2u.getZ() * mm > maxuMm.getZ()) {
                maxuMm.setZ(max2u.getZ() * mm);
            }
        }
    }
    DensityCollection densityCollection = getDatabaseSession().create(DensityCollection.class);
    concreteRevision.eSet(StorePackage.eINSTANCE.getConcreteRevision_DensityCollection(), densityCollection);
    List<org.bimserver.models.store.Density> newList = new ArrayList<>();
    for (Density density : generateGeometry.getDensities()) {
        org.bimserver.models.store.Density dbDensity = StoreFactory.eINSTANCE.createDensity();
        dbDensity.setType(density.getType());
        dbDensity.setDensity(density.getDensityValue());
        dbDensity.setGeometryInfoId(density.getGeometryInfoId());
        dbDensity.setTrianglesBelow(density.getNrPrimitives());
        dbDensity.setVolume(density.getVolume());
        newList.add(dbDensity);
    }
    newList.sort(new Comparator<org.bimserver.models.store.Density>() {

        @Override
        public int compare(org.bimserver.models.store.Density o1, org.bimserver.models.store.Density o2) {
            return Float.compare(o1.getDensity(), o2.getDensity());
        }
    });
    densityCollection.getDensities().addAll(newList);
    // For all revisions created, we need to repopulate the density arrays and make sure those are sorted
    for (Revision rev : result.getRevisions()) {
        long nrTriangles = 0;
        densityCollection = getDatabaseSession().create(DensityCollection.class);
        rev.eSet(StorePackage.eINSTANCE.getRevision_DensityCollection(), densityCollection);
        List<org.bimserver.models.store.Density> newList2 = new ArrayList<>();
        for (ConcreteRevision concreteRevision2 : rev.getConcreteRevisions()) {
            for (org.bimserver.models.store.Density density : concreteRevision2.getDensityCollection().getDensities()) {
                newList2.add(density);
                nrTriangles += density.getTrianglesBelow();
            }
        }
        densityCollection.getDensities().clear();
        newList2.sort(new Comparator<org.bimserver.models.store.Density>() {

            @Override
            public int compare(org.bimserver.models.store.Density o1, org.bimserver.models.store.Density o2) {
                return Float.compare(o1.getDensity(), o2.getDensity());
            }
        });
        densityCollection.getDensities().addAll(newList2);
        rev.eSet(StorePackage.eINSTANCE.getRevision_NrPrimitives(), nrTriangles);
    }
}
Also used : Bounds(org.bimserver.models.geometry.Bounds) ArrayList(java.util.ArrayList) Density(org.bimserver.geometry.Density) Revision(org.bimserver.models.store.Revision) ConcreteRevision(org.bimserver.models.store.ConcreteRevision) ConcreteRevision(org.bimserver.models.store.ConcreteRevision) Vector3f(org.bimserver.models.geometry.Vector3f) DensityCollection(org.bimserver.models.store.DensityCollection)

Aggregations

Density (org.bimserver.geometry.Density)2 ConcreteRevision (org.bimserver.models.store.ConcreteRevision)2 DensityCollection (org.bimserver.models.store.DensityCollection)2 Revision (org.bimserver.models.store.Revision)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 BimserverDatabaseException (org.bimserver.BimserverDatabaseException)1 GenerateGeometryResult (org.bimserver.GenerateGeometryResult)1 GeometryGenerator (org.bimserver.GeometryGenerator)1 SummaryMap (org.bimserver.SummaryMap)1 OidCounters (org.bimserver.database.OidCounters)1 PostCommitAction (org.bimserver.database.PostCommitAction)1 IdEObject (org.bimserver.emf.IdEObject)1 IfcModelInterface (org.bimserver.emf.IfcModelInterface)1 Bounds (org.bimserver.models.geometry.Bounds)1 Vector3f (org.bimserver.models.geometry.Vector3f)1 NewRevisionAdded (org.bimserver.models.log.NewRevisionAdded)1 IfcHeader (org.bimserver.models.store.IfcHeader)1 ModelCheckerInstance (org.bimserver.models.store.ModelCheckerInstance)1 ModelCheckerResult (org.bimserver.models.store.ModelCheckerResult)1