예제 #1
0
 void import_ReportRequest(FeatureImport sender, gView.Framework.system.UI.RequestArgs args)
 {
     args.Result = MessageBox.Show(
         args.Request,
         "Warning",
         args.Buttons,
         MessageBoxIcon.Warning);
 }
예제 #2
0
        private bool ImportToNewFeatureclass(IFeatureDataset destDS, string fcname, IFeatureClass sourceFC, FieldTranslation fieldTranslation, bool project, List <IQueryFilter> filters, DatasetNameCase namecase)
        {
            if (!_cancelTracker.Continue)
            {
                return(true);
            }

            switch (namecase)
            {
            case DatasetNameCase.upper:
                fcname = fcname.ToUpper();
                fieldTranslation.ToUpper();
                break;

            case DatasetNameCase.lower:
                fcname = fcname.ToLower();
                fieldTranslation.ToLower();
                break;

            case DatasetNameCase.classNameUpper:
                fcname = fcname.ToUpper();
                break;

            case DatasetNameCase.classNameLower:
                fcname = fcname.ToLower();
                break;

            case DatasetNameCase.fieldNamesUpper:
                fieldTranslation.ToUpper();
                break;

            case DatasetNameCase.fieldNamesLower:
                fieldTranslation.ToLower();
                break;
            }
            try
            {
                fcname = fcname.Replace(".", "_");

                if (destDS == null)
                {
                    _errMsg = "Argument Exception";
                    return(false);
                }
                IFeatureDatabase fdb = destDS.Database as IFeatureDatabase;
                if (!(fdb is IFeatureUpdater))
                {
                    _errMsg = "Database don't implement IFeatureUpdater...";
                    return(false);
                }

                IDatasetElement destLayer = destDS[fcname] as IDatasetElement;
                if (destLayer != null)
                {
                    if (ReportRequest != null)
                    {
                        RequestArgs args = new RequestArgs(
                            "Featureclass " + fcname + " already exists in database\nDo want to replace it?",
                            MessageBoxButtons.YesNoCancel,
                            DialogResult.Cancel);
                        ReportRequest(this, args);
                        switch (args.Result)
                        {
                        case DialogResult.No:
                            return(true);

                        case DialogResult.Cancel:
                            _errMsg = "Import is canceled by the user...";
                            return(false);
                        }
                    }
                }

                if (destLayer != null)
                {
                    fdb.DeleteFeatureClass(fcname);
                }
                int fcID = fdb.CreateFeatureClass(destDS.DatasetName,
                                                  fcname,
                                                  sourceFC,
                                                  (fieldTranslation == null) ?
                                                  ((sourceFC.Fields != null) ? (IFields)sourceFC.Fields.Clone() : new Fields()) :
                                                  fieldTranslation.DestinationFields);
                if (fcID < 0)
                {
                    _errMsg = "Can't create featureclass " + fcname + "...\r\n" + fdb.lastErrorMsg;
                    destDS.Dispose();
                    return(false);
                }

                destLayer = destDS[fcname] as IDatasetElement;

                if (destLayer == null || !(destLayer.Class is IFeatureClass))
                {
                    _errMsg = "Can't load featureclass " + fcname + "...\r\n" + destDS.lastErrorMsg;
                    destDS.Dispose();
                    return(false);
                }
                IFeatureClass destFC = destLayer.Class as IFeatureClass;

                if (project && destFC.SpatialReference != null && !destFC.SpatialReference.Equals(sourceFC.SpatialReference))
                {
                    _transformer = new GeometricTransformer();
                    //_transformer.FromSpatialReference = sourceFC.SpatialReference;
                    //_transformer.ToSpatialReference = destFC.SpatialReference;
                    _transformer.SetSpatialReferences(sourceFC.SpatialReference, destFC.SpatialReference);
                }

                if (_cancelTracker.Continue)
                {
                    bool result = true;

                    if (fdb is IFeatureImportEvents)
                    {
                        ((IFeatureImportEvents)fdb).BeforeInsertFeaturesEvent(sourceFC, destFC);
                    }

                    if (!_schemaOnly)
                    {
                        result = CopyFeatures(sourceFC, fdb, destFC, fieldTranslation, filters);
                    }
                    if (!result)
                    {
                        fdb.DeleteFeatureClass(fcname);
                        destDS.Dispose();
                        return(false);
                    }

                    if (fdb is IFeatureImportEvents)
                    {
                        ((IFeatureImportEvents)fdb).AfterInsertFeaturesEvent(sourceFC, destFC);
                    }
                }

                if (fdb is IFileFeatureDatabase)
                {
                    if (!((IFileFeatureDatabase)fdb).Flush(destFC))
                    {
                        _errMsg = "Error executing flush for file database...";
                        return(false);
                    }
                }
                destDS.Dispose();

                if (_cancelTracker.Continue)
                {
                    return(true);
                }
                else
                {
                    fdb.DeleteFeatureClass(fcname);
                    _errMsg = "Import is canceled by the user...";
                    return(false);
                }
            }
            finally
            {
                if (_transformer != null)
                {
                    _transformer.Release();
                    _transformer = null;
                }
            }
        }