private async Task <bool> Register() { //Get the active map view. var mapView = MapView.Active; if (mapView?.Map == null) { return(false); } _current_CIMg2g = await mapView.Map.GetGroundToGridCorrection(); if (_current_CIMg2g != null) { _copy_CIMg2g = _current_CIMg2g.CreateCopy(); //make a copy of the G2G right before listening for events } else { _current_CIMg2g = new CIMGroundToGridCorrection(); _copy_CIMg2g = new CIMGroundToGridCorrection(); } GroundToGridEvent.Subscribe(GroundToGridEventMethod); return(true); }
protected override async void OnClick() { //Get the active map view. var mapView = MapView.Active; if (mapView?.Map == null) { return; } var cim_g2g = await mapView.Map.GetGroundToGridCorrection(); if (cim_g2g == null) { cim_g2g = new CIMGroundToGridCorrection(); // CIM for ground to grid is null for new maps, so initialize it for the first time here. } bool bInitialIsCorrecting = cim_g2g.Enabled; bool bInitialUsingDirectionOffset = cim_g2g.UseDirection; double dInitialDirectionOffsetCorrection = cim_g2g.Direction; //NOTE: cim_g2g.GetDirectionOffset(); //this application does not use the helper extension methods //the helper extension methods are best used when READING the settings to avoid having to test for IsEnabled, && UseScale && etc. //the helper extension methods simplify the code and tests for those for you internally. bool bInitialUsingDistanceFactor = cim_g2g.UseScale; bool bInitialUsingConstantScaleFactor = cim_g2g.ScaleType == GroundToGridScaleType.ConstantFactor; //cim_g2g.UsingConstantScaleFactor(); double dInitialScaleFactor = cim_g2g.ConstantScaleFactor; //NOTE: cim_g2g.GetConstantScaleFactor(); //this application does not use the helper extension methods //the helper extension methods are best used when READING the settings to avoid having to test for IsEnabled, && UseScale && etc. //the helper extension methods simplify the code and tests for those for you internally. //changing all the G2G settings cim_g2g.Enabled = !bInitialIsCorrecting; cim_g2g.UseDirection = !bInitialUsingDirectionOffset; cim_g2g.Direction = dInitialDirectionOffsetCorrection + 10; // store and set this in decimal degrees, irrespective of project unit settings cim_g2g.UseScale = !bInitialUsingDistanceFactor; cim_g2g.ScaleType = bInitialUsingConstantScaleFactor ? GroundToGridScaleType.ComputeUsingElevation : GroundToGridScaleType.ConstantFactor; cim_g2g.ConstantScaleFactor = dInitialScaleFactor * 2; await mapView.Map.SetGroundToGridCorrection(cim_g2g); }
protected async override void OnClick() { // get the feature layer that's selected in the table of contents var selectedLayers = MapView.Active.GetSelectedLayers(); var CADLayer = selectedLayers.OfType <FeatureLayer>().FirstOrDefault() as FeatureLayer; string CadFileName = ""; SpatialReference fcSR = null; if (CADLayer == null) { System.Windows.MessageBox.Show("Please select the CAD layer in the table of contents", "Layer Transform"); return; } double dMetersPerUnit = 1; string CadFeatClassNameForSelectedLayer = ""; //Run on MCT bool isValid = await QueuedTask.Run(() => { var CADFeatClass = CADLayer.GetFeatureClass(); var FilePathConnX = CADFeatClass.GetDatastore(); string sCADFilePath = FilePathConnX.GetConnectionString(); sCADFilePath = sCADFilePath.Replace("DATABASE=", ""); var FeatDS = CADFeatClass.GetFeatureDataset(); CadFileName = System.IO.Path.Combine(sCADFilePath, FeatDS.GetName()); string fileExtension = System.IO.Path.GetExtension(CadFileName); fileExtension = fileExtension.ToLower(); string sTargetFileName = System.IO.Path.GetFileNameWithoutExtension(CadFileName); sTargetWldFile = System.IO.Path.Combine(sCADFilePath, sTargetFileName + ".wld"); //get name for layer string FCName = CADFeatClass.GetName(); CadFeatClassNameForSelectedLayer = System.IO.Path.Combine(CadFileName, FCName); bool bIsCAD = fileExtension == ".dwg" || fileExtension == ".dgn" || fileExtension == ".dxf"; FeatureClassDefinition CADFeatClassDef = CADFeatClass.GetDefinition(); fcSR = CADFeatClassDef.GetSpatialReference(); bool bResult = bIsCAD & !fcSR.IsGeographic; //The addin requires that the CAD data is not in geographic coordinates if (bResult) { if (fcSR.IsProjected) { dMetersPerUnit = fcSR.Unit.ConversionFactor; //meters per unit } } return(bResult); }); // if not a valid CAD file if (!isValid) { System.Windows.MessageBox.Show("Please select the CAD layer in the table of contents." + Environment.NewLine + "CAD data in a geographic coordinate system is not supported.", "Transform CAD Layer"); return; } double dOriginX = 0; // Origin Northing(Y) coordinate double dOriginY = 0; // Origin Northing (Y) coordinate double dGridX = 0; // Grid Easting (X) coordinate double dGridY = 0; // Grid Northing (Y) coordinate double dScaleFactor = 1.0000; // Scale Factor double dRotation = 0.0000; // Rotation bool bSetGroundToGrid = false; #region Collect parameters from dialog var transformationDlg = new TransformationInput(); transformationDlg.Owner = FrameworkApplication.Current.MainWindow; TransformationViewModel VM = new TransformationViewModel(); transformationDlg.DataContext = VM; string sLastUsedParams = ""; bool bCancel = false; try { if (transformationDlg.ShowDialog() == true) { string sOriginX = VM.Transformation.OriginX; string sOriginY = VM.Transformation.OriginY; string sGridX = VM.Transformation.GridX; string sGridY = VM.Transformation.GridY; string sScaleFactor = VM.Transformation.ScaleFactor; string sRotation = VM.Transformation.Rotation; string sSetGroundToGrid = VM.Transformation.UpdateGround2Grid.ToString(); sLastUsedParams = sOriginX + "|" + sOriginY + "|" + sGridX + "|" + sGridY + "|" + sScaleFactor + "|" + sRotation + "|" + sSetGroundToGrid; bSetGroundToGrid = VM.Transformation.UpdateGround2Grid; if (!Double.TryParse(sOriginX, out dOriginX)) { MessageBox.Show("Local Easting (X) must be a number." + Environment.NewLine + "Press the Transform button again to retry.", "Local Easting (X)"); return; } if (!Double.TryParse(sOriginY, out dOriginY)) { MessageBox.Show("Local Northing (Y) must be a number." + Environment.NewLine + "Press the Transform button again to retry.", "Local Northing (Y)"); return; } if (!Double.TryParse(sGridX, out dGridX)) { MessageBox.Show("Grid Easting (X) must be a number." + Environment.NewLine + "Press the Transform button again to retry.", "Grid Easting (X)"); return; } if (!Double.TryParse(sGridY, out dGridY)) { MessageBox.Show("Grid Northing (Y) must be a number." + Environment.NewLine + "Press the Transform button again to retry.", "Grid Northing (Y)"); return; } if (!Double.TryParse(sScaleFactor, out dScaleFactor)) { MessageBox.Show("Scale Factor must be a number." + Environment.NewLine + "Press the Transform button again to retry.", "Scale Factor"); return; } if (dScaleFactor <= 0) { MessageBox.Show("Scale Factor must be greater than zero." + Environment.NewLine + "Press the Transform button again to retry.", "Scale Factor"); return; } if (!Double.TryParse(sRotation, out dRotation)) { MessageBox.Show("Rotation must be a number." + Environment.NewLine + "Press the Transform button again to retry.", "Rotation"); return; } if (bSetGroundToGrid) { var mapView = MapView.Active; if (mapView?.Map == null) { return; } var cimG2G = await mapView.Map.GetGroundToGridCorrection(); if (cimG2G == null) { cimG2G = new CIMGroundToGridCorrection(); } cimG2G.UseScale = true; cimG2G.ScaleType = GroundToGridScaleType.ConstantFactor; cimG2G.ConstantScaleFactor = dScaleFactor; cimG2G.UseDirection = true; //use direction offset cimG2G.Direction = dRotation; //direction offset angle cimG2G.Enabled = true; //turn ground to grid ON await mapView.Map.SetGroundToGridCorrection(cimG2G); } } else { // Canceled from dialog bCancel = true; return; } } finally { transformationDlg = null; if (!bCancel) { TransformCADDialog.Default["LastUsedParams"] = sLastUsedParams; TransformCADDialog.Default.Save(); //comment out if you only want to save settings within each app session } } #endregion var pFrom1 = new Coordinate2D(dOriginX, dOriginY); var pTo1 = new Coordinate2D(dGridX, dGridY); var dDeltaX = dGridX - dOriginX; var dDeltaY = dGridY - dOriginY; var pFrom2 = new Coordinate2D(pFrom1.X + (10000 / dMetersPerUnit), pFrom1.Y + (10000 / dMetersPerUnit)); var Rotated = GeometryEngine.Instance.Rotate(pFrom2.ToMapPoint(), pFrom1.ToMapPoint(), dRotation * Math.PI / 180); MapPoint RotatedAndScaled = (MapPoint)GeometryEngine.Instance.Scale(Rotated, pFrom1.ToMapPoint(), dScaleFactor, dScaleFactor); var RotatedScaledTranslated = new Coordinate2D(RotatedAndScaled.X + dDeltaX, RotatedAndScaled.Y + dDeltaY); if (WriteWorldFile(sTargetWldFile, pFrom1, pTo1, pFrom2, RotatedScaledTranslated)) { if (fcSR.IsUnknown && MapView.Active.Map.SpatialReference.IsProjected) {//if .prj is unknown use the active map's projection for the CAD file. fcSR = MapView.Active.Map.SpatialReference; } CancelableProgressorSource cps = new CancelableProgressorSource("Define Projection", "Canceled"); int numSecondsDelay = 5; bool bContinue = true; //The following code re-assigns or assigns the projection so that the CAD layer //can be redrawn in the correct location using the .wld file. await QueuedTask.Run(async() => { cps.Progressor.Max = (uint)numSecondsDelay; //check every second cps.Progressor.Value += 1; cps.Progressor.Message = "Creating and applying world file..."; await DefineProjectionAsync(CadFileName, fcSR, cps); bContinue = !cps.Progressor.CancellationToken.IsCancellationRequested; if (CADLayer.Parent is Layer) { MapView.Active.ZoomTo(CADLayer.Parent as GroupLayer); //Invalidate the layer to refresh display MapView.Active.Invalidate(CADLayer.Parent as GroupLayer, MapView.Active.Extent); } else { MapView.Active.ZoomTo(CADLayer as Layer); MapView.Active.Invalidate(CADLayer as Layer, MapView.Active.Extent); } }, cps.Progressor); if (!bContinue) { return; } } else { MessageBox.Show("The world file could not be created.", "Transform"); return; } }