How create an attribute for guidepoint?


#1

How could I create an attribute? for an array coordinates. This attribute could be a color or whatever helps me know one of them is different from the rest. Hey image

All guidepoints must have same color (or a Boolean attribute; It just have to work) but one of then I’ll choose manually.

require "yaml"
def cnc()
	model = Sketchup.active_model
	ents  = model.entities
	view  = model.active_view
	faces = ents.grep(Sketchup::Face)
	faces.each {|face|
		loop = face.outer_loop
		edgeuses = loop.edgeuses
		pts = []
		edgeuses.each {|use| pts << use.edge.start.position.to_a}
		until pts[0][1] == face.bounds.min[1]
		  pts.rotate!
		end
		pts.each {|pt| ents.add_cpoint(pt)
		#sleep 0.5
		#view.refresh 
		}
	}
end



def cnc_menu()
    
    tools_menu = UI.menu("Tools")
    tools_menu.add_separator
    
offest_all_faces_item = tools_menu.add_item("CNC New Project") {
cnc()
}
end

cnc_menu()

#2

Please post code correctly in the forum:


You can identify Sketchup::Drawingelement subclasses by checking and saving a persistent ID between sessions …

Any entity subclass object can have an attribute dictionary created for it …

# At top of Plugin Module
DICT = "YourCompanyName_YourPluginName"

# some where in the code
def add_attr( obj, key, value )
  obj.set_attribute( DICT, key, value )
end

# Elsewhere calling the method using a ConstructionPoint reference ...
add_attr( cpoint, "Position", cpoint.position )
add_attr( cpoint, "Type", "Start" )

#3

This is my code again but it didn’t work for me


require "yaml"

require "sketchup.rb"

#require "extensions.rb"

# At top of Plugin Module

DICT = "havanaapplegod_cncnew"

def cnc()

	model = Sketchup.active_model

	ents  = model.entities

	view  = model.active_view

	faces = ents.grep(Sketchup::Face)

	faces.each {|face|

		loop = face.outer_loop

		edgeuses = loop.edgeuses

		pts = []

		edgeuses.each {|use| pts << use.edge.start.position.to_a

			#add_attr( pts[0], "Type", "Start" )

		}

		print "PTS =", pts

		until pts[0][1] == face.bounds.min[1]

		  pts.rotate!

		end

		pts.each {|pt| ents.add_cpoint(pt)

			add_attr( ents,  "Type", "Start" )

		#sleep 0.5

		#view.refresh 

		}

	}

end

def add_attr( obj, key, value ) 

	obj.set_attribute( DICT, key, value )

end

def cnc_menu()

    

    tools_menu = UI.menu("Tools")

    tools_menu.add_separator

    

    

    offest_all_faces_item = tools_menu.add_item("CNC New Mother Fucker") {

        

        cnc()

        

    }

   

    

end

cnc_menu()


]Error: #<NoMethodError: undefined method `set_attribute' for #<Sketchup::Entities:0x007fa5cff7e3e8>>

/Users/joemac/Library/Application Support/SketchUp 2018/SketchUp/Plugins/CNCNew/cncnew.rb:51:in `add_attr'

/Users/joemac/Library/Application Support/SketchUp 2018/SketchUp/Plugins/CNCNew/cncnew.rb:41:in `block (2 levels) in cnc'

/Users/joemac/Library/Application Support/SketchUp 2018/SketchUp/Plugins/CNCNew/cncnew.rb:40:in `each'

/Users/joemac/Library/Application Support/SketchUp 2018/SketchUp/Plugins/CNCNew/cncnew.rb:40:in `block in cnc'

/Users/joemac/Library/Application Support/SketchUp 2018/SketchUp/Plugins/CNCNew/cncnew.rb:20:in `each'

/Users/joemac/Library/Application Support/SketchUp 2018/SketchUp/Plugins/CNCNew/cncnew.rb:20:in `cnc'

/Users/joemac/Library/Application Support/SketchUp 2018/SketchUp/Plugins/CNCNew/cncnew.rb:62:in `block in cnc_menu'

SketchUp:1:in `call'


#4

After reading the error message, you verify it by looking up the API documentation and not finding Sketchup::Entities#set_attribute. So either another method should have been called, or that method should have been called on a single entity.


#5

You are trying to add the attribute to the entities collection, I think you want to add it to a cpoint.
Try this code change:

pts.each{|pt|
  cpt = ents.add_cpoint(pt)
  add_attr( cpt,  "Type", "Start" )
}

Although I don’t see where you use it later anyway ??


#6

How can I access to cpoint attributes?

I list all entities on my model but I can’t see those points listed.

ents.each{| ent | print “\n ent : “, ent
}


#7

You can make it simpler, but this explains the steps…
Note that using
ents = model.active_entities
to work in all contexts, not just the base
model.entities

First:

cps = ents.grep(Sketchup::ConstructionPoint) 
# array of all cpoints

Now process that array to find just those cpoints with the matching attribute.

starts = cps.find_all{|cp| cp.get_attribute(DICT, 'Type', '???') == 'Start' } 
# where 'starts' is an array of any of those cpoints
# that have their 'Type' set to 'Start'

#8

… also it doesn’t make much sense to assign all of the cpoints as the start. There is usually 1 start, 1 end, and intervening points along the arc.


#9

Update 0.8 check this out

require "yaml"
require "sketchup.rb"
#require "extensions.rb"

# At top of Plugin Module
DICT = "havanaapplegod_cncnew"

def cnc()
	model = Sketchup.active_model

	
	ents =	model.active_entities

	view  = model.active_view

	faces = ents.grep(Sketchup::Face)

	faces.each {|face|

		loop = face.outer_loop

		edgeuses = loop.edgeuses

		pts = []

		edgeuses.each {|use| pts << use.edge.start.position.to_a
	
		}



		print "PTS =", pts

		until pts[0][1] == face.bounds.min[1]
		  pts.rotate!
		end
		
		ptfirst = 0

		pts.each {|pt| ents.add_cpoint(pt)

			cpt = ents.add_cpoint(pt)

			if ptfirst == 0
				add_attr( cpt,  "Type", true )
			else
		  		add_attr( cpt,  "Type", false )
			end
			#sleep 0.5
			#view.refresh 
			print "\nptfirst ==", ptfirst
			ptfirst += 1
		}
		# Check the Attribute

		cps = ents.grep(Sketchup::ConstructionPoint)
		# array of all cpoints

		starts = cps.find_all{|cp| cp.get_attribute(DICT, 'Type', '???') == true }

		# where 'starts' is an array of any of those cpoints
		# that have their 'Type' set to 'Start'
		
		starts.each {|attr| 

			print "\n The Attribute ==", attr 
		
		}

	}
end

def add_attr( obj, key, value ) 
	obj.set_attribute( DICT, key, value )
end

def cnc_menu()
    
    tools_menu = UI.menu("Tools")
    tools_menu.add_separator
    
    offest_all_faces_item = tools_menu.add_item("CNC New BETA 0.7") {
        
        cnc()
        
    }
    
end

cnc_menu()
———— Console Output ——
PTS =[[27.183060952514595, 22.17309422937775, 0.0], [27.183060952514595, 29.662646004791227, 0.0], [36.49626760787338, 29.662646004791227, 0.0], [36.49626760787338, 44.62585013488956, 0.0], [49.63581685802641, 44.62585013488956, 0.0], [49.63581685802641, 22.17309422937775, 0.0]]
ptfirst ==0
ptfirst ==1
ptfirst ==2
ptfirst ==3
ptfirst ==4
ptfirst ==5
 The Attribute ==#<Sketchup::ConstructionPoint:0x007fb9c0e86d00>

It seems to be working property but clarify if I’m wrong; I do this listing

starts.each {|attr|  print "\n The Attribute ==", attr }

Then I got this result; is 0x007fb9c0e86d00 a memory pointer? Or encrypted property? anyway how can I get the value inside the attribute.

The Attribute ==#<Sketchup::ConstructionPoint:0x007fb9c0e86d00>

#10

Use the get_attribute() method.


#11

Please note that in your code:

When you printing out you are printing the ConstructionPoint itself with his unique ID.

To get the attribute you have to ‘ask’ it:

starts.each {|cpoint| 
  attr = cpoint.get_attribute(DICT, 'Type', '???')
  print "\n The Attribute ==", attr
}