Select to view content in your preferred language

How can i clone a graphic object?

4525
4
02-27-2012 11:26 AM
ChrisSpring
Emerging Contributor
How can I clone a Graphic object?
something along the lines of

var newGraphic:Graphic = ObjectUtil.clone(exisitngGraphic)



Thanks
Chris
Tags (2)
0 Kudos
4 Replies
ChrisSpring
Emerging Contributor
I've found this post very helpful, but I still need to set the attributes and geometry after I execute the clone...

 
var newGraphic:Graphic = deepClone(existingGraphic);
newGraphic.attributes = existingGraphic.attributes;
newGraphic.geometry = existingGraphic.geometry;
 


This works, but I'm still setting the 2 properties of the graphic as references pointing at my first graphic, so its not a fully cloned object.
0 Kudos
IvanBespalov
Frequent Contributor
cspring,

http://forums.arcgis.com/threads/31993-Can-t-add-the-same-Graphic-twice-to-a-GraphicsLayer?p=107453#...

Question to you: Can you tell, what is the base idea? Why do you need in 2 absolutely equals graphics?
0 Kudos
ChrisSpring
Emerging Contributor
cspring,

http://forums.arcgis.com/threads/31993-Can-t-add-the-same-Graphic-twice-to-a-GraphicsLayer?p=107453#...

Question to you: Can you tell, what is the base idea? Why do you need in 2 absolutely equals graphics?


I have more than one layer, and my situation requires a copy of an existing graphic. I also want to know if its even possible to clone a graphic like I can clone other flex objects.
Chris
0 Kudos
IvanBespalov
Frequent Contributor
You can write some utility for cloning graphics with own validation logic.

Sample clone utility:
package ee.alphagis.utils
{
 import com.esri.ags.Graphic;
 import com.esri.ags.geometry.Geometry;
 import com.esri.ags.symbols.Symbol;
 
 import mx.core.IFactory;

 public class MyGraphicUtils
 {
  public static function cloneGraphic(graphic:Graphic):Graphic
  {
   if (!graphic)
   {
    return null;
   }
   
   var geometry:Geometry = graphic.geometry;
   var symbol:Symbol = graphic.symbol;
   var attributes:Object = graphic.attributes;
   var autoMoveToTop:Boolean = graphic.autoMoveToTop;
   var checkForMouseListeners:Boolean = graphic.checkForMouseListeners;
   var infoWindowRenderer:IFactory = graphic.infoWindowRenderer;
   
   var result:Graphic = new Graphic(geometry, symbol, attributes);
   result.autoMoveToTop = autoMoveToTop;
   result.checkForMouseListeners = checkForMouseListeners;
   result.infoWindowRenderer = infoWindowRenderer;
   
   return result;
  }
 }
}


Utility usage sample:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
      xmlns:s="library://ns.adobe.com/flex/spark" 
      xmlns:mx="library://ns.adobe.com/flex/mx">
 <!-- Adobe FLEX SDK ver. 4.5.1 -->
        <!-- ArcGIS API for FLEX ver. 2.5 -->
 <s:layout>
  <s:VerticalLayout paddingBottom="20"
        paddingLeft="20"
        paddingRight="20"
        paddingTop="20" />
 </s:layout>

 <fx:Script>
  <![CDATA[
   import com.esri.ags.Graphic;
   import com.esri.ags.SpatialReference;
   import com.esri.ags.geometry.MapPoint;
   import com.esri.ags.symbols.SimpleMarkerSymbol;
   
   import ee.alphagis.utils.MyGraphicUtils;
   
   import mx.controls.Alert;
   import mx.utils.StringUtil;
   
   private var _pattern:String = "base={0}, geometry={1}, name={2}, stamp={3}\n\nclone={4}, geometry={5}, name={6}, stamp={7}";
   
   protected function button1_clickHandler(event:MouseEvent):void
   {
    var attr:Object = new Object();
    attr.name = "qwerty";
    attr.date = new Date();
    var geometry:MapPoint = new MapPoint(654, 456, new SpatialReference(4326));
    var symbol:SimpleMarkerSymbol = new SimpleMarkerSymbol("circle", 16, 0x00ff56);
    var baseGraphic:Graphic = new Graphic(geometry, symbol, attr);
    
    var clonedGraphic:Graphic = MyGraphicUtils.cloneGraphic(baseGraphic);
    
    var message:String = StringUtil.substitute(_pattern, 
     baseGraphic.toString(),
     baseGraphic.geometry.type, 
     baseGraphic.attributes["name"], 
     baseGraphic.attributes["date"],
     clonedGraphic.toString(),
     clonedGraphic.geometry.type, 
     clonedGraphic.attributes["name"],
     clonedGraphic.attributes["date"]);
    Alert.show(message);
   }
  ]]>
 </fx:Script>
 
 <s:Button label="clone" 
     click="button1_clickHandler(event)"/>
 
</s:Application>
0 Kudos