{"id":1546,"date":"2006-06-04T00:17:13","date_gmt":"2006-06-04T07:17:13","guid":{"rendered":"https:\/\/developer.microsoft.com\/en-us\/office\/blogs\/?p=1546"},"modified":"2021-11-17T12:17:24","modified_gmt":"2021-11-17T20:17:24","slug":"getting-to-know-your-neighbors-quickly","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/getting-to-know-your-neighbors-quickly\/","title":{"rendered":"Getting to Know Your Neighbors \u2013 Quickly"},"content":{"rendered":"<p style=\"font-weight: 400\">Visio&#8217;s\u00a0<a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/devref\/HTML2\/DAR_Properties_(S)_1687.asp\">SpatialNeighbors<\/a>\u00a0property is useful for finding shapes that are nearby other shapes or that overlap other shapes.\u00a0\u00a0It can be used to programmatically answer the question &#8220;What did I drop my shape on top of?&#8221;.<\/p>\n<p style=\"font-weight: 400\">Unfortunately, the SpatialNeighbors property can be extremely slow under certain circumstances.\u00a0\u00a0In real world scenarios, searching might take anywhere from a second to over a minute.\u00a0\u00a0That&#8217;s unacceptable for developers who need to make frequent calls to the method.\u00a0\u00a0Two factors can significantly alter the performance of the method.<\/p>\n<p style=\"font-weight: 400\">First, searching group shapes is much slower than searching non-group shapes.\u00a0\u00a0This has to do with the way that SpatialNeighbors defines containment.\u00a0\u00a0Shape A is contained within Group Shape B if A is contained within the union of all the sub-shapes of B.\u00a0\u00a0Thus a lot of work is done to calculate that union before any relationship determination is made.\u00a0\u00a0It may not be possible to eliminate groups, but it is always helpful to performance to minimize the number of sub-shapes as much as possible.\u00a0\u00a0Also make sure to avoid nested groups.\u00a0\u00a0Opt instead for of a single group shape with many sub-shapes.\u00a0\u00a0Reducing the number of groups or shapes within groups can double or triple the performance of SpatialNeighbors.<\/p>\n<p style=\"font-weight: 400\">That&#8217;s a decent improvement, but the second factor is even more important for performance.\u00a0\u00a0With each call to SpatialNeighbors, the developer must specify the Relation types they wish to detect.\u00a0\u00a0The four relation types are ContainedIn, Contain, Overlap and Touching.\u00a0\u00a0Any combination of types can be searched for.\u00a0\u00a0The problem is that Visio checks for each of these relationships individually and must redo all the analysis work described above every time.\u00a0\u00a0This leads to astronomic execution times.<\/p>\n<p style=\"font-weight: 400\">The workaround here is to not specify the relation type at all.\u00a0\u00a0When passing in 0 for the Relation parameter, Visio makes several internal optimizations because it no longer cares about the particular way in which two shapes are related.\u00a0\u00a0For developers who don&#8217;t care about the relation type, passing in 0 is a couple orders of magnitude faster (100x) than passing in all the types.\u00a0\u00a0For those that really do need to find the shapes matching a particular relationship, call SpatialNeighbors passing in 0 to get a collection of candidate shapes.\u00a0\u00a0Then iterate through the collection and use the\u00a0<a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/devref\/HTML2\/DAR_Properties_(S)_1683.asp\">SpatialRelation\u00a0<\/a>property to get the relationship.\u00a0\u00a0This technique uses the really fast searching code to weed out all the shapes that are completely unrelated.\u00a0\u00a0The expensive code to determine actual relationships is then used with a much smaller set of shapes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Visio&#8217;s SpatialNeighbors property is useful for finding shapes that are nearby other shapes or that overlap other shapes.  It can be used to programmatically answer the question &#8220;What did I drop my shape on top of?&#8221;. This blog discusses the SpatialNeighbors property.<\/p>\n","protected":false},"author":69200,"featured_media":25159,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[11],"tags":[77],"class_list":["post-1546","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-office-add-ins","tag-visio"],"acf":[],"blog_post_summary":"<p>Visio&#8217;s SpatialNeighbors property is useful for finding shapes that are nearby other shapes or that overlap other shapes.  It can be used to programmatically answer the question &#8220;What did I drop my shape on top of?&#8221;. This blog discusses the SpatialNeighbors property.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/1546","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/users\/69200"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/comments?post=1546"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/1546\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/media\/25159"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/media?parent=1546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/categories?post=1546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/tags?post=1546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}